RRegexBuilder
Get RegexBuilder

RegexBuilder · regex answers

Regex for a MAC address

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

Six hex pairs separated by colons or hyphens. As written the separator may change mid-address; if you need consistency, capture the first separator and backreference it: ^([0-9a-f]{2})([:-])(?:[0-9a-f]{2}\2){4}[0-9a-f]{2}$.

What each part does

[0-9a-f]{2}one octet as two hex digits
[:-]colon or hyphen separator
{5}five octet-and-separator pairs, then a final octet

Tested against real input

Every row below was run through the engine when this page was built. The third column is the substring the pattern actually matched.

InputResultMatched
00:1A:2B:3C:4D:5Ematches00:1A:2B:3C:4D:5E
00-1a-2b-3c-4d-5ematches00-1a-2b-3c-4d-5e
aa:bb:cc:dd:ee:ffmatchesaa:bb:cc:dd:ee:ff
00:1A:2B:3C:4Dno match
001A2B3C4D5Eno match
00:1A:2B:3C:4D:5E:6Fno match

The mistake to avoid

Forgetting Cisco's dotted-triplet form 001a.2b3c.4d5e, which is a perfectly normal way to print a MAC address and matches none of the above.

Try it on your own text

Paste this pattern and your input, see each match highlighted with its capture groups, step through what the engine did, and get the equivalent syntax for JavaScript, Python, Java, Go and PCRE.

Open RegexBuilder

Other regex answers