RegexBuilder · regex answers
Regex for a MAC address
/^([0-9a-f]{2}[:-]){5}[0-9a-f]{2}$/iSix 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.
| Input | Result | Matched |
|---|---|---|
| 00:1A:2B:3C:4D:5E | matches | 00:1A:2B:3C:4D:5E |
| 00-1a-2b-3c-4d-5e | matches | 00-1a-2b-3c-4d-5e |
| aa:bb:cc:dd:ee:ff | matches | aa:bb:cc:dd:ee:ff |
| 00:1A:2B:3C:4D | no match | — |
| 001A2B3C4D5E | no match | — |
| 00:1A:2B:3C:4D:5E:6F | no 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 RegexBuilderOther regex answers
- Regex for an email address
- Regex for a US phone number
- Regex for an IPv4 address
- Regex for a URL
- Regex for a date in YYYY-MM-DD format
- Regex to match digits only
- Regex for alphanumeric characters
- Regex to find special characters
- Regex for a strong password
- Regex to match whitespace
- Regex to trim leading and trailing whitespace
- Regex for a UUID