Back to the tool

Regex for MAC Address Validation

You are probably skimming, so here is the pattern: ^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$. Six pairs of hex digits, colons or hyphens between them, nothing else in the string. That is the regex for MAC address validation that every guide publishes, and for most jobs it is where you should stop reading. But it has a flaw that almost nobody mentions, and the stricter version is worth the extra ten characters. Keep going if you validate MACs at a trust boundary. Leave now if you are checking a form field.

The regex for MAC address validation, piece by piece

Breaking the standard pattern down token by token:

^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$

^ and $ anchor the match to the whole string, so xx00:11:22:33:44:55yy fails instead of matching the middle. [0-9A-Fa-f]{2} matches exactly two hexadecimal digits, upper or lower case. [:-] matches one separator, colon or hyphen. The group ([0-9A-Fa-f]{2}[:-]) repeated {5} times consumes five pairs plus their separators, and the final ([0-9A-Fa-f]{2}) consumes the sixth pair with no trailing separator. Five separators plus six pairs is the shape of every EUI-48 MAC address.

Test it against the classics: AA:BB:CC:DD:EE:FF passes, aa-bb-cc-dd-ee-ff passes, 01:23:45:67:89:AB passes. AA:BB:CC:DD:EE fails, five pairs. GG:HH:II:JJ:KK:LL fails, G is not hex. All correct.

The flaw: mixed separators sail through

Here is what the standard pattern accepts that it should not: 00:11-22:33-44:55. Colons in some positions, hyphens in others. The [:-] character class allows either separator at every position independently, so any mixture passes. No real device writes a MAC address like that. If your validator accepts it, you are storing garbage that will fail the next system downstream, the one with the strict check.

The fix is a backreference. Capture the first separator, then require every later separator to be the same one:

^([0-9A-Fa-f]{2})([:-])(?:[0-9A-Fa-f]{2}\2){4}[0-9A-Fa-f]{2}$

The ([:-]) group grabs whichever separator appears first, and \2 repeats exactly that character at the remaining four positions. Now 00:11:22:33:44:55 passes, 00-11-22-33-44-55 passes, and 00:11-22:33-44:55 fails. In Python:

import re
mac_re = re.compile(r'^([0-9A-Fa-f]{2})([:-])(?:[0-9A-Fa-f]{2}\2){4}[0-9A-Fa-f]{2}$')
print(bool(mac_re.match("00:11:22:33:44:55")))   # True
print(bool(mac_re.match("00:11-22:33-44:55")))  # False

My opinion: use the strict version anywhere the MAC address crosses a trust boundary, device provisioning, firewall rules, inventory imports. Use the loose version for a form field where a human is typing and you will normalize afterward. Strictness belongs at the door, not in the hallway.

The Cisco dotted format is a different pattern entirely

Cisco equipment writes MACs as three groups of four hex digits with dots: 001A.2B3C.4D5E. Neither pattern above matches it, and it needs its own:

^[0-9a-fA-F]{4}\.[0-9a-fA-F]{4}\.[0-9a-fA-F]{4}$

Note the escaped dots. An unescaped . matches any character, which would let 001Ax2B3Cy4D5E through. Functionally the dotted form is the same 48 bits, just regrouped, so if you accept multiple formats, normalize to one before storing: lowercase everything, pick colons, and keep the database consistent.

The one thing to keep

Copy the standard pattern for quick checks, the backreference version for anything that matters, and remember that regex proves shape, not identity. A valid-looking MAC can still belong to nobody.

Frequently asked questions

What is the standard regex for MAC address validation?

^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$ matches six pairs of hex digits separated by colons or hyphens. Its one known flaw is accepting mixed separators like 00:11-22:33-44:55.

How do you stop a MAC regex from accepting mixed separators?

Capture the first separator and backreference it: ^([0-9A-Fa-f]{2})([:-])(?:[0-9A-Fa-f]{2}\2){4}[0-9A-Fa-f]{2}$. Every separator must match the first one.

What is the regex for the Cisco dotted MAC address format?

^[0-9a-fA-F]{4}\.[0-9a-fA-F]{4}\.[0-9a-fA-F]{4}$ matches three groups of four hex digits separated by dots, like 001A.2B3C.4D5E. Escape the dots or they match anything.

Can a regex confirm a MAC address is real?

No. A regex checks the shape, not the identity. The first three octets are the vendor OUI; confirming one belongs to a real manufacturer means looking it up in the IEEE OUI registry.

Should you normalize a MAC address before validating it?

It usually helps. Lowercasing and converting separators to colons before validation means one simple pattern covers everything and stored data stays consistent. Validate strictly at the door, normalize inside.

Related reading: What Is the Regex for UUID Validation? The Loose One and the Strict One · Regex to Validate an IPv4 Address · Regex Word Boundaries, Explained With Examples · Email Regex Validation: the Mistakes Everyone Makes

Try the tool

Need the pattern explained in plain English? Open the regex template builder and customize the MAC template with your own format rules.

Get new free tools by email

Want the next guide in your inbox? I publish one practical guide per new tool. Subscribe to the free newsletter on Substack. No spam, unsubscribe anytime.