A MAC address regex

Today I worked on a form and script used for registering users on a restricted-access wireless network. Here’s a nice compact regex for checking that MAC addresses have been entered in the correct format. (If you’re using this in a double-quoted PHP string, escape the “$” with a backslash.)

/^([0-9A-F]{2}:){5}[0-9A-F]{2}$/i

Bjoern commented on Mon Jun 4 09:09:14 2007:

Hi,

great regex! exactly what I was looking for.

It only does not match lower case also the windows output of ipconfig/all (using a dash as delimiter) is not covered

here’s my addition: /^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$/i


Paul commented on Mon Jun 4 10:13:26 2007:

Thanks for the update, Bjoern. The “/i” modifier at the end already makes it case-insensitive (assuming you’re using Perl-compatible regexes), but adding the dash as a separator is a good improvement.


Jeremy commented :

Thanks for this!

Added support for no separator…

/^([0-9A-F]{2}[:-]{0,1}){5}[0-9A-F]{2}$/i


Manohar Bhattarai commented :

This link might give you all more insight. http://manoharbhattarai.wordpress.com/2012/02/17/regex-to-match-mac-address/


Manohar commented :

Great regex. I have a link that explains to match all 3 types of mac addresses http://manoharbhattarai.wordpress.com/tag/regex-to-find-mac-address/



Share: