How to Match a MAC Address with Regex
A Media Access Control (MAC) address is a unique identifier assigned to a network interface controller. It consists of six groups of two hexadecimal digits, typically separated by colons (:) or hyphens (-).
The Pattern Breakdown
The regular expression ^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$ cleanly validates this structure:
[0-9A-Fa-f]{2}: Matches exactly two hexadecimal digits.[:-]: Matches the separator (either a colon or a hyphen).{5}: Repeats the digit+separator sequence exactly five times.([0-9A-Fa-f]{2}): Matches the final two hex digits, which do not have a trailing separator.
Common Use Cases
- Device Tracking: Parsing DHCP server logs to identify unique devices.
- Network Security: Validating inputs for MAC filtering rules on routers and firewalls.