Skip to content
Regex for MAC Address — Pattern Explained with Examples

Regex for MAC Address — Pattern Explained with Examples

DodaTech Updated Jun 20, 2026 2 min read

MAC (Media Access Control) address validation is needed in network tools, device enrollment systems, and hardware inventory management. A MAC address is a 48-bit identifier typically written as 6 pairs of hexadecimal digits separated by colons or hyphens, or as a continuous 12-character hex string.

The Pattern

/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/

For the continuous no-separator format:

/^[0-9A-Fa-f]{12}$/

Pattern Breakdown

PartMeaning
^Start-of-string anchor
([0-9A-Fa-f]{2}[:-]){5}Five groups of two hex digits each followed by colon or hyphen
[0-9A-Fa-f]{2}Final sixth group of two hex digits
$End-of-string anchor

Matches

  • 00:1A:2B:3C:4D:5E
  • 00-1A-2B-3C-4D-5E
  • AA:BB:CC:DD:EE:FF
  • a1:b2:c3:d4:e5:f6
  • 001A2B3C4D5E

Does NOT Match

  • 00:1A:2B:3C:4D (only 5 groups)
  • 00:1A:2B:3C:4D:5E:6F (7 groups)
  • 00:1A:2B:3C:4D:GH (invalid hex characters)
  • 00:1A:2B:3C:4D:5 (odd number of hex digits in last group)
  • 001A2B3C4D5 (only 11 hex characters)

Language Examples

JavaScript

const macRegex = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/;
const macContinuousRegex = /^[0-9A-Fa-f]{12}$/;
console.log(macRegex.test('00:1A:2B:3C:4D:5E')); // true
console.log(macRegex.test('00:1A:2B:3C:4D'));     // false

Python

import re
pattern = r'^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$'
print(bool(re.match(pattern, '00:1A:2B:3C:4D:5E')))  # True
print(bool(re.match(pattern, '00:1A:2B:3C:4D')))      # False

Go

package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$`)
	fmt.Println(re.MatchString("00:1A:2B:3C:4D:5E"))  // true
	fmt.Println(re.MatchString("00:1A:2B:3C:4D"))       // false
}

Common Pitfalls

  • The multicast vs unicast bit (second hex character’s least significant bit) determines address type — this pattern does not validate that bit
  • EUI-64 identifiers are 8 bytes (16 hex digits) long, not 6 bytes — a different pattern is needed for IPv6-derived MACs
  • The broadcast address FF:FF:FF:FF:FF:FF is valid by the pattern but may need special handling in business logic
  • Case sensitivity can cause duplicate records — normalize to uppercase or lowercase before storage
  • Leading zeros in each pair are significant and must be preserved

Real-World Use Cases

  • Device provisioning portals — validate MAC addresses during network device enrollment and registration
  • Inventory management — scan and verify hardware MAC addresses against purchase records or asset tags
  • DHCP server configuration — validate MAC address entries for static IP reservations in DHCP lease files

FAQ

What is the difference between a MAC address and an EUI-64?
A MAC address is 48 bits (6 hex pairs). EUI-64 is 64 bits (8 hex pairs) used in IPv6 stateless autoconfiguration. The middle 16 bits (FF:FE) are inserted into the MAC to create an EUI-64.
Should I store MAC addresses in uppercase or lowercase?
Choose one convention and normalize consistently. Uppercase is more readable for hardware labels; lowercase is more common in programming contexts. The important thing is to compare case-insensitively.

Related Patterns

Regex for IPv4 Address Regex for IPv6 Address

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro