Mac Address Regex Python Validator

Search...

⌘K

Mac Address Regex Python Validator

Search...

⌘K


Mac Address Regex Python Validator

Mac Address Regex Python Validator

Use the MAC Address Regex Python Validator to test and validate MAC address formats using Python’s re module. This tool is essential for developers working on network configuration, device authentication, or IoT systems. Combine it with the IP Address Regex Python Validator for dual-layer validation in network-heavy apps, or integrate it with the UUID Regex Python Validator when handling device identifiers across systems.

bb:aa:dd:aa:55:55
Possible security issues
This regex appears to be safe.
Explanation
  • [A-Z]: uppercase letters
  • [a-z]: lowercase letters
  • [0-9]: digits
  • \.: a literal dot
  • +: one or more of the preceding
  • *: zero or more of the preceding
  • ?: optional (zero or one)
  • ^: start of string
  • $: end of string
Match information
Match 1: "bb:aa:dd:aa:55:55" at index 0
Test your APIs today!

Write in plain English — Qodex turns it into secure, ready-to-run tests.

Regular Expression - Documentation

What is a MAC Address Regex?


A MAC (Media Access Control) address is a unique identifier assigned to a device’s network interface. It typically looks like 01:23:45:67:89:AB or 01-23-45-67-89-AB, composed of six pairs of hexadecimal digits.


To validate MAC addresses using regex, we use a pattern that ensures the correct grouping, delimiter, and character format.


MAC Address Regex Pattern (Python)


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


This pattern validates:

  • 6 hexadecimal pairs separated by either colons (:) or hyphens (-)

  • Both uppercase and lowercase letters (A-F, a-f)

  • Strict format with no extra characters


How to Validate MAC Address in Python


Here’s how you can use the re module in Python to validate MAC addresses:


import re

def is_valid_mac(mac):
    pattern = re.compile(r'^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$')
    return bool(pattern.match(mac))

# Example usage
macs = ["01:23:45:67:89:AB", "01-23-45-67-89-AB", "0123.4567.89AB"]
for mac in macs:
    print(f"{mac} => {is_valid_mac(mac)}")


Examples


Valid

  • AA:BB:CC:DD:EE:FF

  • aa-bb-cc-dd-ee-ff


Invalid

  • AA:BB:CC:DD:EE

  • AABB.CCDD.EEFF

  • GG:HH:II:JJ:KK:LL


Use Cases


  • IoT and Embedded Systems: Validate MAC addresses during device provisioning.

  • Network Security: Filter and whitelist specific MAC formats in firewalls.

  • Form Validation: Ensure valid MAC input in Python-based admin tools or APIs.

  • Batch File Processing: Use alongside the Python Regex Tester for cleaning large datasets with device records.


Pro Tips


  • Flexible Delimiters: If your input could contain both : and -, keep the current pattern. For one specific format only, remove the alternate.

  • Case Insensitivity: This pattern works for both uppercase and lowercase letters. No need for .lower() conversions.

  • Additional Check: Pair with the IP Address Regex Python Validator to fully verify network device entries.

  • Avoid Typos: Add a pre-validation step to strip extra whitespace using .strip() in Python before applying the regex.


Frequently asked questions

Does this regex support MAC addresses in dot-separated formats like Cisco’s AAAA.BBBB.CCCC?×
No, it only validates colon or hyphen-separated MACs. Dot formats require a different pattern.
Can I validate both lowercase and uppercase letters?+
What if the MAC address is missing a separator?+
Is this regex enough for secure network authentication?+
Can I integrate this in Python APIs or CLI tools?+