A MAC address (Media Access Control address) is a unique identifier assigned to a network interface controller (NIC) for use as a network address in communications within a network segment. It is used as an identifier for a specific device on the network. In this article let’s understand how we can create a regex for MAC Address and how regex can be matched for a valid MAC Addresses.
Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.
Structure of a MAC Address
A MAC address should have the following criteria and structure-
- It must contain 12 hexadecimal digits.
- One way to represent them is to form six pairs of the characters separated with a hyphen (-) or colon(:). For example, 01-23-45-67-89-AB is a valid MAC address.
- Another way to represent them is to form three groups of four hexadecimal digits separated by dots(.). For example, 0123.4567.89AB is a valid MAC address.
Regex for checking if MAC Address is valid or not
Regular Expression-
/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})|([0-9a-fA-F]{4}\\.[0-9a-fA-F]{4}\\.[0-9a-fA-F]{4})$/gm
Test string examples for the above regex-
Input String | Match Output |
---|---|
3263.6543.31VD | does not match |
04:33:66:32:12:AD | matches |
45-23-64-12-BB | does not match |
32:12:00:23:42:AA | matches |
Here is a detailed explanation of the above regex-
^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})|([0-9a-fA-F]{4}\\.[0-9a-fA-F]{4}\\.[0-9a-fA-F]{4})$/gm
1st Alternative ^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})
^ asserts position at start of a line
1st Capturing Group ([0-9A-Fa-f]{2}[:-]){5}
{5} matches the previous token exactly 5 times
A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
Match a single character present in the list below [0-9A-Fa-f]
{2} matches the previous token exactly 2 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
A-F matches a single character in the range between A (index 65) and F (index 70) (case sensitive)
a-f matches a single character in the range between a (index 97) and f (index 102) (case sensitive)
Match a single character present in the list below [:-]
:- matches a single character in the list :- (case sensitive)
2nd Capturing Group ([0-9A-Fa-f]{2})
Match a single character present in the list below [0-9A-Fa-f]
{2} matches the previous token exactly 2 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
A-F matches a single character in the range between A (index 65) and F (index 70) (case sensitive)
a-f matches a single character in the range between a (index 97) and f (index 102) (case sensitive)
2nd Alternative ([0-9a-fA-F]{4}\\.[0-9a-fA-F]{4}\\.[0-9a-fA-F]{4})$
3rd Capturing Group ([0-9a-fA-F]{4}\\.[0-9a-fA-F]{4}\\.[0-9a-fA-F]{4})
Match a single character present in the list below [0-9a-fA-F]
{4} matches the previous token exactly 4 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
a-f matches a single character in the range between a (index 97) and f (index 102) (case sensitive)
A-F matches a single character in the range between A (index 65) and F (index 70) (case sensitive)
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
. matches any character (except for line terminators)
Match a single character present in the list below [0-9a-fA-F]
{4} matches the previous token exactly 4 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
a-f matches a single character in the range between a (index 97) and f (index 102) (case sensitive)
A-F matches a single character in the range between A (index 65) and F (index 70) (case sensitive)
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
. matches any character (except for line terminators)
Match a single character present in the list below [0-9a-fA-F]
{4} matches the previous token exactly 4 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
a-f matches a single character in the range between a (index 97) and f (index 102) (case sensitive)
A-F matches a single character in the range between A (index 65) and F (index 70) (case sensitive)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
Hope this article was useful to check if the string is a valid MAC Address or not. In conclusion, a MAC address serves as a unique identifier for network devices, crucial for effective communication within a network segment. We explored how regex can be employed to validate MAC addresses, aiding in pattern matching and enhancing network management and security.