How to Use Regex to Match a MAC Address
Introduction
A MAC address (Media Access Control address) is a unique identifier assigned to network interfaces for communications on a network. If you’re working with networking, security, or log analysis, you might need a regular expression (regex) to extract or validate MAC addresses from text. This guide provides regex patterns for different MAC address formats and explains how they work.
What is a MAC Address?
A MAC address consists of six pairs of hexadecimal digits (0-9, A-F), typically separated by colons, hyphens, or sometimes written without separators. Traditional MAC addresses are 12-digit (6 bytes or 48 bits) hexadecimal numbers. By convention, these addresses are usually written in one of the following three formats, although there are variations:
- XX:XX:XX:XX:XX:XX (colon-separated)
- XX-XX-XX-XX-XX-XX (hyphen-separated)
- XXX.XXX.XXX.XXX (dot-separated)
Regex Patterns for Matching MAC Addresses
Colon-Separated MAC Addresses
This pattern ensures exactly six groups of two hexadecimal characters with colons as separators.
([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}
Hyphen-Separated MAC Addresses
Similar to the previous regex but uses hyphens as separators.
([0-9A-Fa-f]{2}-){5}[0-9A-Fa-f]{2}
Dot-Separated MAC Addresses
This regex matches MAC addresses written in three groups of four hexadecimal characters separated by dots.
([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4}
MAC Addresses with Any Separator
This regex pattern combines all the previously used patterns into a single one, allowing colons, hyphens, or dots as valid separators.
([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}|([0-9A-Fa-f]{2}-){5}[0-9A-Fa-f]{2}|([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4}
Common Mistakes and Edge Cases
1. Case Sensitivity Issues:
Some formats use uppercase letters (A-F), while others allow lowercase (a-f). If your regex is case-sensitive, it might incorrectly reject valid addresses.
Solution: Ensure your regex accounts for both uppercase and lowercase characters by either expanding the character set (e.g., [0-9A-Fa-f]) or enabling a case-insensitive flag/mode.
2. Incorrect Use of Separators
Some regex patterns may accidentally allow inconsistent separators, like 00-1A:2B-3C:4D:5E, which is not a valid MAC address format.
Solution: Ensure the regex strictly enforces a single type of separator.
3. Allowing Non-Hex Characters
Some regex patterns mistakenly allow non-hexadecimal characters (A-Z, special symbols) if not properly constrained.
Solution: Ensure the regex only permits [0-9A-Fa-f] in MAC address sections.
4. Missing Leading Zeros
Some applications require MAC addresses to always have two characters per segment (e.g., 01:23:45:67:89:AB), but user input might be formatted incorrectly as 1:23:45:67:89:AB.
Solution: Ensure each segment strictly has two hexadecimal characters.
5. Not Accounting for Dot-Separated MAC Addresses
Some MAC regex patterns only validate colon and hyphen-separated formats but do not consider the XXXX.XXXX.XXXX format used by some vendors.
Solution: Use a separate regex pattern for dot-separated MAC addresses.
Testing Your MAC Address Regex
To ensure your regex pattern works correctly, it’s always a good idea to test it, which you can do with the following tools:
- Regex Forge: A macOS desktop app designed for an interactive and visual regex testing experience.
- RegexBuddy: A Windows desktop app with powerful debugging features for regex.
- Regexr: An online regex tester that allows you to quickly check your pattern in a browser.
Conclusion
A well-crafted regex for MAC addresses can help you efficiently validate or extract MAC addresses from various sources. Whether you’re working with network logs, parsing device configurations, or writing security scripts, these patterns will get the job done.
Looking for an intuitive way to work with regex on macOS? Try Regex Forge, a feature-packed regex testing tool designed for Mac developers.