Regex to Match MAC Addresses

Validate MAC addresses using this regex pattern. Matches both colon-separated and hyphen-separated hexadecimal byte format. Free regex tool.

Regular Expression

/([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}/gi

Token Breakdown

TokenDescription
(Start of capturing group
[0-9a-fA-F]Character class — matches any one of: 0-9a-fA-F
{2}Matches exactly 2 times
[:-]Character class — matches any one of: :-
)End of group
{5}Matches exactly 5 times
[0-9a-fA-F]Character class — matches any one of: 0-9a-fA-F
{2}Matches exactly 2 times

Detailed Explanation

This regex matches MAC (Media Access Control) addresses in their standard notation. Here is the token-by-token breakdown:

( — Opens a capturing group for the first five pairs of hex digits, each followed by a separator.

[0-9a-fA-F]{2} — A character class matching exactly two hexadecimal digits. It includes digits 0-9, lowercase a-f, and uppercase A-F. Each pair represents one byte of the six-byte MAC address.

[:-] — A character class matching either a colon or a hyphen. MAC addresses are commonly written with either separator style: AA:BB:CC:DD:EE:FF or AA-BB-CC-DD-EE-FF. This class accepts both formats.

){5} — Closes the capturing group and repeats it exactly five times. This matches the first five byte-pairs, each with a trailing separator.

[0-9a-fA-F]{2} — Matches the sixth and final byte-pair of two hexadecimal digits, without a trailing separator.

The g flag enables global matching to find all MAC addresses in the text, and the i flag makes the hex letters case-insensitive. Note that this pattern allows mixed separators (e.g., AA:BB-CC:DD-EE:FF), which is technically non-standard. If strict consistency is needed, you would use two separate patterns or a backreference. This pattern covers the most common MAC address formats used in networking tools, DHCP configurations, and device identification.

Example Test Strings

InputExpected
00:1A:2B:3C:4D:5EMatch
AA-BB-CC-DD-EE-FFMatch
00:1A:2B:3C:4DNo Match
GG:HH:II:JJ:KK:LLNo Match
01:23:45:67:89:abMatch

Try It — Interactive Tester

//gi
gimsuy

Match Highlighting(3 matches)

00:1A:2B:3C:4D:5E AA-BB-CC-DD-EE-FF 00:1A:2B:3C:4D GG:HH:II:JJ:KK:LL 01:23:45:67:89:ab

Matches & Capture Groups

#100:1A:2B:3C:4D:5Eindex 0
Group 1:4D:
#2AA-BB-CC-DD-EE-FFindex 18
Group 1:EE-
#301:23:45:67:89:abindex 69
Group 1:89:
Pattern: 37 charsFlags: giMatches: 3

Ctrl+Shift+C to copy regex

Customize this pattern →