regexhelper
Test a pattern. Copy a ready-made one. No signup.

Regex to Match a MAC Address

Copy the pattern, paste your text below, and see every MAC address it finds. Below the tester: how it accepts both colon and hyphen separators.

Regex pattern

/\b(?:[0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}\b/g

This matches a MAC address as six hex pairs separated by colons or hyphens, e.g. 00:1A:2B:3C:4D:5E. It accepts either separator but not the dotted Cisco 0000.0000.0000 form.

/ /

What this pattern matches

This matches a 48-bit MAC address written as six pairs of hex digits separated by colons or hyphens.

How it works, part by part

[0-9a-fA-F]{2} — one octet: two hex digits.

[:-] — a colon or hyphen separator.

(?:…){5} — five octet-plus-separator groups, then one final octet.

Mixed separators

This will technically accept a mix of colons and hyphens in one address. If you need consistency, capture the first separator with a backreference: ([:-])(?:[0-9a-fA-F]{2}\1){4}….

Use it in your code

Python
import re
text = "Valid: 01:23:45:67:89:ab, 01-23-45-67-89-AB. Invalid: 01:23:45:67:89 (only 5 octets)."
pattern = r'''\b(?:[0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}\b'''
for m in re.finditer(pattern, text):
    print(m.group())
JavaScript
const text = "Valid: 01:23:45:67:89:ab, 01-23-45-67-89-AB. Invalid: 01:23:45:67:89 (only 5 octets).";
const re = new RegExp(String.raw`\b(?:[0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}\b`, "g");
console.log(text.match(re));

FAQ

Does it accept hyphen-separated MACs?

Yes. The [:-] class allows both 01:23:… and 01-23-… forms.

Will it match a 5-octet string?

No. It requires exactly six octets, so a shorter run fails the boundary.

How do I forbid mixed separators?

Use a capture group and backreference so every separator must match the first one.