Regex for Matching a MAC Address
Matches a MAC address as six colon-separated pairs of hex digits.
Published October 11, 2026
This regex matches MAC addresses written with colons or hyphens, such as 00:1A:2B:3C:4D:5E.
How the pattern works
Breaking the pattern down: [0-9A-Fa-f]{2} = one hex byte; [:-] = colon or hyphen; {5} = five byte groups then the last byte.
Test your regex against both matching and non-matching inputs; use the Regex tool on this site to experiment live.
Pattern tests
/^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$/
✓ '00:1A:2B:3C:4D:5E'
✓ '00-1a-2b-3c-4d-5e'
✗ '00:1A:2B:3C:4D'
✗ '00:1A:2B:3C:4D:5G'
Practical examples
The pattern
const re = /^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$/Test it
const re = /^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$/
re.test('00:1A:2B:3C:4D:5E') // true
re.test('00-1a-2b-3c-4d-5e') // true
re.test('00:1A:2B:3C:4D') // false
re.test('00:1A:2B:3C:4D:5G') // falseIn Python
import re
re.search(r'^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$', text)Common mistakes
- Allowing mixed separators.
- Cisco dotted format (0012.3456.789a).
- Forgetting case.
Key takeaways
- Anchors (^ and $) make the whole string match instead of a part of it.
- Regex checks format only; verify data separately when it matters.
- Test with valid and invalid samples.
FAQ
Mixed separators?
Use a backreference to force the same separator.
Where can I test this pattern?
Use the Regex Tester on this site, or regex101.com.