0

I'm trying to use regex to extract a MAC address from a string. The regex that I've used should work.

raw_mac_string returns a ether <mac_address>

What I'm trying to do is get original_mac to extract the <mac_address> from raw_mac_string.

original_mac when run, keeps giving error: AttributeError: 'NoneType' object has no attribute 'group'

Thanks for the help

I guess my question is, how can I extract the MAC address from a string that contains other text as well?

#get original mac address
    global original_mac
    raw_mac_string = subprocess.check_output('ifconfig en0 | grep ether | awk "{print $2}"', shell=True)
    print "raw_mac"+raw_mac_string
    original_mac = re.match(r"/^(?:[[:xdigit:]]{2}([-:]))(?:[[:xdigit:]]{2}\1){4}[[:xdigit:]]{2}$/", raw_mac_string).group()
magna_nz
  • 1,243
  • 5
  • 23
  • 42
  • Your regexp only matches if the whole string is the MAC address. If `raw_mac_string` is `ether `, it won't match because of `ether` at the beginning. – Barmar Apr 10 '15 at 00:09
  • So how can I change my regex so that it skips the words that aint the MAC address? – magna_nz Apr 10 '15 at 00:19
  • Use a lookbehind that matches `^ether ` but doesn't include it in the match. – Barmar Apr 10 '15 at 00:22

0 Answers0