
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.
Mac Address Regex Python Validator - 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. This address is assigned to network interface controllers—such as WiFi adapters, Ethernet cards, and similar hardware—serving as a fingerprint that distinguishes each device on a local network.
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 redef 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)}")
Extracting a MAC Address from a String
To pull a MAC address from a larger chunk of text, you can use Python's re module with a suitable regular expression. Here’s how you might do it:
import redef extract_mac_address(s): pattern = r'(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}' return re.findall(pattern, s)
Example usage
log_entry = "Unknown error in node 00:00:5e:00:53:af. Terminating." found_macs = extract_mac_address(log_entry) print(found_macs) # Output: ['00:00:5e:00:53:af']
This approach will scan the text and return any MAC addresses it finds, making it useful when you need to extract such information from logs or unstructured messages.
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?
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?
Related Articles



Test your APIs today!
Write in plain English — Qodex turns it into secure, ready-to-run tests.



