DevKits

Validation

Match a MAC address

Match MAC addresses in colon or hyphen separated form.

Pattern

Regular expression

(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}

How it works

Matches the two most common MAC address formats: colon-separated (Linux/macOS) and hyphen-separated (Windows). Six pairs of hex digits with a consistent separator.

Matches

  • 00:1A:2B:3C:4D:5E
  • 00-1A-2B-3C-4D-5E
  • ff:ff:ff:ff:ff:ff

Non-matches

  • 00:1A:2B:3C:4D (5 groups)
  • GG:1A:2B:3C:4D:5E

Common gotchas

This accepts a mix of separators (`00:1A-2B:3C:4D:5E`). To force consistency, use a capture group backreference: `([0-9A-Fa-f]{2})([:-])(?:[0-9A-Fa-f]{2}\\2){4}[0-9A-Fa-f]{2}`.

Language snippets

JavaScript

const re = /(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}/g;

Python

re.findall(r"(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}", text)

Go

re := regexp.MustCompile(`(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}`)

Java

Pattern.compile("(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}")

Frequently asked questions

What is the regex for MAC address?

The pattern (?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2} matches MAC address. Matches the two most common MAC address formats: colon-separated (Linux/macOS) and hyphen-separated (Windows). Six pairs of hex digits with a consistent separator.

What does this MAC address pattern match?

It matches strings like 00:1A:2B:3C:4D:5E, 00-1A-2B-3C-4D-5E, ff:ff:ff:ff:ff:ff, but rejects 00:1A:2B:3C:4D (5 groups), GG:1A:2B:3C:4D:5E.

How do I use this pattern in JavaScript, Python, Go, Java?

Ready-to-run snippets are provided for JavaScript, Python, Go, Java. Copy the one for your language from the Language snippets section — each wraps the same core pattern in that language's regex API.

What are common pitfalls with a MAC address regex?

This accepts a mix of separators (`00:1A-2B:3C:4D:5E`). To force consistency, use a capture group backreference: `([0-9A-Fa-f]{2})([:-])(?:[0-9A-Fa-f]{2}\\2){4}[0-9A-Fa-f]{2}`.

Related patterns

Try this pattern in a tool

Try any pattern live in the Regex Tester