Pattern library / Validation
MAC Address Regex (EUI-48)
A MAC address is six groups of two hex digits — aa:bb:cc:dd:ee:ff — and every tool has a separator opinion: Windows shows dashes, Linux shows colons, some capture tools nothing at all. This pattern accepts colon and dash forms in one shot; the notes cover the no-separator and Cisco dotted variants.
How the pattern works
([0-9a-fA-F]{2}[:-])five times, then one final pair — the count is the validation: 48 bits, six bytes.[:-]accepts both separators but also mixed ones (aa:bb-cc…) — strict environments hardcode one literal:^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$.- No-separator form:
^[0-9a-fA-F]{12}$. Cisco dotted (001a.2b3c.4d5e):^([0-9a-fA-F]{4}\.){2}[0-9a-fA-F]{4}$. - Locally-administered and multicast flags live in the first byte — useful as a sanity check in network tooling, never something to encode in the regex.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
aa:bb:cc:dd:ee:ff | Match | lowercase, colon-separated (Linux) | Try → |
AA-BB-CC-DD-EE-FF | Match | uppercase, dash-separated (Windows) | Try → |
00-1A-2B-3C-4D-5E | Match | vendor OUI with digits | Try → |
aabb.ccdd.eeff | No match | Cisco dotted format — alternative in notes | Try → |
aabbccddeeff | No match | no separators — different pattern | Try → |
aa:bb:cc:dd:ee | No match | five groups only | Try → |
Where to use it
JavaScript
const MAC = /^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$/;
MAC.test('aa:bb:cc:dd:ee:ff'); // true
MAC.test('aa:bb:cc:dd:ee'); // false
Python (audit a list)
import re
MAC = r'^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$'
valid = [l.strip() for l in open('macs.txt') if re.fullmatch(MAC, l.strip())]
Bash (grep, colon-only)
grep -E '^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$' arp.txt
Common mistakes
- In the Cisco variant the dot must be escaped (
\.) or001aX2b3cX4d5ematches — the class form[:-]needs no escape because every character in it is literal. - Case handling:
[0-9a-fA-F]is inclusive; lowercase-only inventories can tighten to[0-9a-f]for stricter comparison. - EUI-64 (eight pairs, for IPv6 interfaces like
00:11:22:33:44:55:66:77) needs {7} groups — a different pattern, not a tweak of this one.
FAQ
How do I match MAC addresses without separators?
^[0-9a-fA-F]{12}$ — exactly twelve hex digits. Convert incoming formats to one canonical form (usually lowercase colon-separated) before comparing anything.
Why does my pattern only match five groups?
The repeat count is {5} plus a final pair: six pairs total. A {4} or {6} copy-paste is the classic off-by-one — count the groups in your failing input first.
Does this work in GA4 or BigQuery?
Yes — plain classes and quantifiers, valid RE2. Device inventory usually lives in logs or BigQuery rather than GA4, but the pattern is portable everywhere.
Related patterns
IPv4 Address Regex — Validate Dots & Octets · UUID Regex — Match Any UUID (1-5) Format · Log Level Regex — Match INFO / WARN / ERROR Lines
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.