Regex to match MAC Address

 

Hi everyone,

I am back after a long time. In this short blog I will show you how I found the MAC Address of my Ubuntu Linux system using regular expression. You can use this regex to match all 3 commonly used MAC Addresses.

What is a valid MAC address?

A MAC address is a unique identifier assigned to most network adapters or network interface cards (NICs) by the manufacturer for identification, IEEE 802 standards use 48 bits or 6 bytes to represent a MAC address. This format gives 281,474,976,710,656 possible unique MAC addresses.

IEEE 802 standards define 3 commonly used formats to print a MAC address in hexadecimal digits:

  • Six groups of two hexadecimal digits separated by hyphens (-), like 01-23-45-67-89-ab
  • Six groups of two hexadecimal digits separated by colons (:), like 01:23:45:67:89:ab
  • Three groups of four hexadecimal digits separated by dots (.), like 0123.4567.89ab

Now, the regex I used are :

(([0-9A-Fa-f][0-9A-Fa-f][-:]){5}[0-9A-Fa-f][0-9A-Fa-f])|(([0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f].){2}[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f])

OR you can use :

(([0-9A-Fa-f]{2}[-:]){5}[0-9A-Fa-f]{2})|(([0-9A-Fa-f]{4}.){2}[0-9A-Fa-f]{4})

The second one is short and crisp.

I checked it on my Ubuntu Linux system as :

manohar@manohar-desktop:~$ ifconfig eth0 | egrep –only-matching ‘(([0-9A-Fa-f]{2}[-:]){5}[0-9A-Fa-f]{2})|(([0-9A-Fa-f]{4}.){2}[0-9A-Fa-f]{4})’

If you got a better way please post on the comments part.

Thank you.


Regards,
Manohar Bhattarai (मनोहर भट्टराई)

I am Manohar Bhattarai, a vivid java programmer. I am a keen Linux user. I currently boot Ubuntu Linux. I am a Free/Libre Open Source Software enthusiast.

Posted in Linux, Ubuntu Tagged with: , , , , , , , , , , , , , , , , ,
4 comments on “Regex to match MAC Address
  1. yuki86 says:

    thanks:)

  2. creo says:

    Exactly what I needed to sanitize MAC addresses. I really should start learning how to build my own REGEX strings 😉 Thanks for sharing!

  3. Thank you.
    For those not well versed in regex use like my self.
    I found in php I had to add a delimiter to the beginning and end of the string to use it with preg_match(“(([0-9A-Fa-f][0-9A-Fa-f][-:]){5}[0-9A-Fa-f][0-9A-Fa-f])|(([0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f].){2}[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f])/”, $macToTest).
    I also found http://www.regexr.com/ helpful in learning regex.
    Jim

  4. Claudiu says:

    Try this:

    ifconfig -a | grep -iE “([0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2})”

    By using the “i” option tells grep to ignore the case, thus simplifying the regex.

5 Pings/Trackbacks for "Regex to match MAC Address"
  1. events says:

    events…

    […]Regex to match MAC Address « Manohar Bhattarai[…]…

  2. remote administration says:

    […]Regex to match MAC Address « Manohar Bhattarai[…]…

  3. Pogo says:

    […]Regex to match MAC Address « Manohar Bhattarai[…]…

  4. music says:

    […]Regex to match MAC Address « Manohar Bhattarai[…]…

  5. […] have moved this post to my new blog site. Click here to get to the […]

Leave a Reply