Web
MAC Address Regex
A MAC address identifies a network interface and is usually written as six pairs of hexadecimal digits separated by colons, as on Linux and macOS, or by dashes, as on Windows. This pattern accepts both separators and either letter case, which suits device registration portals, router admin tools, inventory spreadsheets and DHCP reservation forms. Note that it allows mixed separators in one address, such as 00:1A-2B:3C:4D:5E. The honest limit is that it does not accept the Cisco dotted style 001a.2b3c.4d5e or 64-bit EUI-64 identifiers.
Pattern (JavaScript)
/^(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$/Starts at the start of the line/string, then (a hexadecimal digit, exactly 2 times, then one of the characters :-), exactly 5 times, then a hexadecimal digit, exactly 2 times, then the end of the line/string.
Examples
Matches
- ✓ 00:1A:2B:3C:4D:5E
- ✓ 00-1a-2b-3c-4d-5e
- ✓ AA:BB:CC:DD:EE:FF
Does not match
- ✗ 00:1A:2B:3C:4D
- ✗ 001A.2B3C.4D5E
- ✗ 00:1G:2B:3C:4D:5E
Try it
0 matches (JavaScript engine)
00:1A:2B:3C:4D:5E
00-1a-2b-3c-4d-5e
AA:BB:CC:DD:EE:FF
00:1A:2B:3C:4D
001A.2B3C.4D5E
00:1G:2B:3C:4D:5ECode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$/g;
const text = "00:1A:2B:3C:4D:5E";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$')
text = '00:1A:2B:3C:4D:5E'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$/';
$text = '00:1A:2B:3C:4D:5E';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$");
String text = "00:1A:2B:3C:4D:5E";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$`)
text := "00:1A:2B:3C:4D:5E"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$");
var text = "00:1A:2B:3C:4D:5E";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$").unwrap();
let text = "00:1A:2B:3C:4D:5E";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$/
text = '00:1A:2B:3C:4D:5E'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I require the same separator throughout?
- Capture the first separator and reuse it with a backreference, for example ^[0-9A-Fa-f]{2}([:-])(?:[0-9A-Fa-f]{2}\1){4}[0-9A-Fa-f]{2}$. Go and Rust do not support backreferences, so check consistency in code there.
- Can I accept the Cisco dotted format?
- Add an alternative such as ^[0-9A-Fa-f]{4}\.[0-9A-Fa-f]{4}\.[0-9A-Fa-f]{4}$. Normalising every format to colon pairs before storing keeps comparisons simple.
- Does a match mean the address belongs to a real device?
- No. Any six hex pairs match, including randomised addresses that phones use for privacy. The pattern only checks the notation.
Related patterns
- IPv4 Address^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$
- IPv6 Address^(?:(?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}|(?:[0-9A-Fa-f]{1,4}:){1,7}:|(?:[0-9A-Fa-f]{1,4}:){1,6}:[0-9A-Fa-f]{1,4}|(?:[0-9A-Fa-f]{1,4}:){1,5}(?::[0-9A-Fa-f]{1,4}){1,2}|(?:[0-9A-Fa-f]{1,4}:){1,4}(?::[0-9A-Fa-f]{1,4}){1,3}|(?:[0-9A-Fa-f]{1,4}:){1,3}(?::[0-9A-Fa-f]{1,4}){1,4}|(?:[0-9A-Fa-f]{1,4}:){1,2}(?::[0-9A-Fa-f]{1,4}){1,5}|[0-9A-Fa-f]{1,4}:(?::[0-9A-Fa-f]{1,4}){1,6}|:(?:(?::[0-9A-Fa-f]{1,4}){1,7}|:))$
- TCP/UDP Port Number^(?:6553[0-5]|655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{0,3})$
- Data URI^data:([a-z]+/[a-z0-9.+-]+)?(?:;[a-z0-9-]+=[a-z0-9-]+)*(;base64)?,[A-Za-z0-9+/=%._~-]*$