DD
DevDash

Regex for MAC Address

Regex Pattern

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

MAC address with colons or dashes as separators

Quick Answer

The regex pattern for mac address is `^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$`. MAC address with colons or dashes as separators. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
00:1B:44:11:3A:B7✓ Matches
00-1B-44-11-3A-B7✓ Matches
aa:bb:cc:dd:ee:ff✓ Matches
00:1B:44:11:3A✗ No match
001B44113AB7✗ No match
00:1B:44:11:3A:G7✗ No match

Code Examples

javascript

const regex = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/;
const isValid = regex.test(value);

python

import re
pattern = r'^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/', $value)) {
    echo "valid";
}

java

String pattern = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.