Mac address regex PHP
Mac address is a unique identifier assigned to network interface controllers like WiFi routers, Ethernet controllers, etc. It has a format of six groups of 2 hexadecimal digits separated by dash or colon (e.g. 00:00:5e:00:53:af). Mac address regular expression can be used to validate that a certain string contains mac address or extract mac address from a given string.
Simple Mac address regex (IEEE 802)
Below is a simple mac address regex that supports dashes or colons as separators:
"/^(?:[0-9A-Fa-f]{2}[:-]){5}(?:[0-9A-Fa-f]{2})$/"
Enter a text in the input above to see the result
Example code in Python:
// Validate mac address
$mac_address_validation_regex = "/^(?:[0-9A-Fa-f]{2}[:-]){5}(?:[0-9A-Fa-f]{2})$/";
echo preg_match($mac_address_validation_regex, '00:00:5e:00:53:af'); // returns 1
// Extract mac addresses from a string
$extract_mac_addresses_pattern = "/(?:[0-9A-Fa-f]{2}[:-]){5}(?:[0-9A-Fa-f]{2})/";
$string_to_match = 'Unknown error in node 00:00:5e:00:53:af. Terminating.';
preg_match_all($extract_mac_addresses_pattern, $string_to_match, $matches);
print_r($matches[0]) // matches[0] is ['00:00:5e:00:53:af']