Regex for MAC address
MAC address with colon or hyphen separators.
The pattern
^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$
What it matches
00:1A:2B:3C:4D:5E00-1A-2B-3C-4D-5Eff:ff:ff:ff:ff:ff
What it doesn't match
001A2B3C4D5E00:1A:2B:3C:4D00:1A:2B:3C:4D:5E:6F
Notes & gotchas
Six pairs of hex digits separated by colons or hyphens. Cisco-style dotted format (0123.4567.89ab) is not matched.
Code in your language
Use the explainer's Code tab to generate ready-to-paste snippets in JavaScript, Python, Java, .NET, Go, Ruby, and PHP for this pattern.
Open in explainer →About this pattern
Identifying and validating user identity (names, contact info, IDs) is one of the most common reasons developers reach for regex. The pattern below handles the format check; for full validation always confirm against a source of truth (database, API, or document).
Quick usage in different languages
Once you've validated a candidate value matches this pattern, you'll typically use it inside your application code. Each language has its own regex syntax:
- JavaScript:
new RegExp(pattern).test(value) - Python:
re.match(pattern, value)with raw strings:r"pattern" - Java:
Pattern.compile(pattern).matcher(value).matches() - C# / .NET:
Regex.IsMatch(value, pattern) - Go:
regexp.MustCompile(pattern).MatchString(value)— Go uses RE2 so some advanced features aren't available - Ruby:
value =~ /pattern/orpattern.match?(value) - PHP:
preg_match('/pattern/', $value)
The explainer's Code tab generates these for any pattern you paste — including the right escaping and idioms for each language.
Common pitfalls
- Anchors matter. If the pattern uses
^and$it expects the entire input to match. To find this pattern inside a longer text, remove the anchors and use the/gflag. - Case sensitivity. Letter ranges like
[A-Z]only match uppercase. Use theiflag or[A-Za-z]for case-insensitive matching. - Escape user input. If you're building a regex from a string variable, escape regex metacharacters first to avoid bugs or injection.
- Performance. For this specific pattern the risk is low, but be cautious of nested quantifiers when adapting it — they can cause exponential backtracking on adversarial input.
See also
Browse all 300 patterns in the library, or open this regex in the interactive explainer for a token-by-token breakdown, live testing, and code in seven languages.
Want more patterns? Browse the full library →