What is a regular expression for a MAC Address?
In this format:
3D:F2:C9:A6:B3:4F
or:
3D-F2-C9-A6-B3-4F
The standard (IEEE 802) format for printing MAC-48 addresses in human-friendly form is six groups of two hexadecimal digits, separated by hyphens
-
or colons:
.
So:
^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$
A little hard on the eyes, but this:
/^(?:[[:xdigit:]]{2}([-:]))(?:[[:xdigit:]]{2}\1){4}[[:xdigit:]]{2}$/
will enforce either all colons or all dashes for your MAC notation.
(A simpler regex approach might permit A1:B2-C3:D4-E5:F6
, for example, which the above rejects.)
This regex matches pretty much every mac format including Cisco format such as 0102-0304-abcd
^([[:xdigit:]]{2}[:.-]?){5}[[:xdigit:]]{2}$
Example strings which it matches:
01:02:03:04:ab:cd
01-02-03-04-ab-cd
01.02.03.04.ab.cd
0102-0304-abcd
01020304abcd
Mixed format will be matched also!
delimiter: ":","-","."
double or single: 00 = 0, 0f = f
/^([0-9a-f]{1,2}[\.:-]){5}([0-9a-f]{1,2})$/i
or
/^([0-9a-F]{1,2}[\.:-]){5}([0-9a-F]{1,2})$/
exm: 00:27:0e:2a:b9:aa, 00-27-0E-2A-B9-AA, 0.27.e.2a.b9.aa ...