SlothGR
SlothGR

Reputation: 145

Python regex extract MAC addresses from string

I need help writing a regular expression, using the python re engine to:

  1. Extract all MAC addresses from a text file
  2. Extract all strings with the following format: foo bar ... MAC:ADDRESS ... baz bat \r\n

Thanks in advance!

I tried the following to extract MAC addresses, without luck:

import re
p = re.compile(ur'((?:(\d{1,2}|[a-fA-F]{1,2}){2})(?::|-*)){6}')
test_str = u"TEXT WITH SOME MAC ADDRESSES 00:24:17:b1:cc:cc TEXT CONTINUES WITH SOME MORE TEXT 20:89:86:9a:86:24"

found = re.findall(p, test_str)
for a in found:
    print a

Upvotes: 3

Views: 31281

Answers (8)

TheCableGUI
TheCableGUI

Reputation: 124

Kinda unrelated but i stumbled across this and i found a solution

mac_pattern = r"\b\w\w-\w\w-\w\w-\w\w-\w\w-\w\w\b"
mac_pattern2 = r"\b\w\w.\w\w.\w\w.\w\w.\w\w.\w\w\b"

so it reads as such:

  1. word boundary
  2. two word characters
  3. a dash
  4. two word characters
  5. dash
  6. two word characters
  7. dash
  8. two word characters
  9. dash
  10. two word characters
  11. dash
  12. two word characters
  13. word boundary.

This works for arp -a in windows 10. Obviously just swap out the dashes for the semicolons in your case.

Upvotes: 0

The best way to find the valid MAC addresses from any input is:

r'([0-9A-Fa-f]{2}[:]){5}([0-9A-Fa-f]{2})[\s]'

Upvotes: -1

Thomas Juul Dyhr
Thomas Juul Dyhr

Reputation: 101

Best regex match of a single MAC address without overflow at the end:

import re

regex = r"^((([a-f0-9]{2}:){5})|(([a-f0-9]{2}-){5}))[a-f0-9]{2}$"

test_str = "89:89:89:89:89:89"

subst = ""

# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 1, re.IGNORECASE)

if result:
    print (result)

Ref: https://regexpattern.com/mac-address/

Upvotes: 0

rahul deepak
rahul deepak

Reputation: 1

import re
print(re.search("([a-f0-9A-F]{4}[.]){2}[a-f0-9A-F]{4}", "0000.aaaa.bbbb").group())

[a-f0-9A-F]{4} -> matches for four occurrences of a-f or 0-9, A-F 

here it will search only for one mac in a string.if you want to search for more than one occurance we need to use re.findall.

Upvotes: -1

Ali Katkar
Ali Katkar

Reputation: 557

text = "this is aa:bb:cc:dd:01:02 test for aa-bb-cc-dd-ee-ff and AABBCCDDEEFF is a mac address without separator"

let us extract mac addresses

def extract_mac_address(text):
    pattern = '(([0-9a-fA-F]{2}[:]){5}([0-9a-fA-F]{2})|([0-9a-fA-F]{2}[-]){5}([0-9a-fA-F]{2})|[0-9a-fA-F]{12})'
    mac_addr_list = re.findall(pattern, text)
    return list(map(lambda x: x[0], mac_addr_list))


print(extract_mac_address(text))

and output is ['aa:bb:cc:dd:01:02', 'aa-bb-cc-dd-ee-ff', 'AABBCCDDEEFF']

Upvotes: 0

JoErNanO
JoErNanO

Reputation: 2488

I have concocted the following: ([0-9a-fA-F]:?){12} to match MAC addresses in the text.

Here is how it is supposed to work:

  • [0-9a-fA-F] matches the characters used to represent hexadecimal numbers
  • :? matches an optional colon
  • (...){12} - all of this is then grouped and repeated 12 times. 12 because a MAC address consists of 6 pairs of hexadecimal numbers, separated by a colon

You can see it in action here .

The Python code then becomes:

import re
p = re.compile(r'(?:[0-9a-fA-F]:?){12}')
test_str = u"TEXT WITH SOME MAC ADDRESSES 00:24:17:b1:cc:cc TEXT CONTINUES WITH SOME MORE TEXT 20:89:86:9a:86:24"

re.findall(p, test_str)

Producing as a result:

[u'00:24:17:b1:cc:cc', u'20:89:86:9a:86:24']

Upvotes: 13

rohantmp
rohantmp

Reputation: 31

I had to match MAC addresses too and this worked:((?:[\da-fA-F]{2}[:\-]){5}[\da-fA-F]{2})

I tested it with this live regex tester: https://regex101.com/#python It had a very nice breakdown of what each piece of regex did.

Upvotes: 3

vks
vks

Reputation: 67978

([0-9a-f]{2}(?::[0-9a-f]{2}){5})

Try this.See demo.

http://regex101.com/r/kP8uF5/5

import re
p = re.compile(ur'([0-9a-f]{2}(?::[0-9a-f]{2}){5})', re.IGNORECASE)
test_str = u"TEXT WITH SOME MAC ADDRESSES 00:24:17:b1:cc:cc TEXT CONTINUES WITH SOME MORE TEXT 20:89:86:9a:86:24"

re.findall(p, test_str)

Upvotes: 9

Related Questions