DebugPointer
Published on

Regex for MAC Address validation

Regex for MAC Address validation

An IP address is a numerical label that is assigned to every device connected to a computer network that uses the Internet Protocol for communication. An IP address serves two main functions: it identifies the host or network interface, and it provides the location of the host in the network.

IP addresses are typically written in dotted-decimal notation, with four numbers separated by periods, such as 192.168.0.1. Each number can range from 0 to 255. The IP address uniquely identifies the device on the network, allowing data to be sent to and from the correct device.

IPv4, or Internet Protocol version 4, is the fourth version of the Internet Protocol, the communications protocol that is used for transmitting data over a network. It is the most widely used version of the IP, and it is the basis for most of the internet's infrastructure.

IPv4 addresses are 32 bits long and are written in dotted-decimal notation, with four numbers separated by periods, such as 192.168.0.1. Each number can range from 0 to 255. The IP address uniquely identifies the device on the network, allowing data to be sent to and from the correct device.

IPv4 has been in use for many years, and it has proven to be a robust and effective protocol. However, the pool of available IPv4 addresses is running out, due to the rapid growth of the internet. This is one of the main reasons why IPv6, the next version of the IP, was developed. IPv6 addresses are longer and use a different format, but they provide a much larger pool of available addresses.

There are two main versions of IP addresses: IP version 4 (IPv4) and IP version 6 (IPv6). IPv4 is the most widely used version, but it is running out of available addresses due to the rapid growth of the internet. IPv6 addresses are longer and use a different format, but they provide a much larger pool of available addresses. In this article let's understand how we can create a regex for IPV6 Address and how regex can be matched for a valid IP 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-

/^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/gm

Test string examples for the above regex-

Input StringMatch Output
34.2.123.999does not match
222.222.222.222matches
aa.231.asd.12does not match
0.0.0.0matches

Here is a detailed explanation of the above regex-

/^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/gm

1st Capturing Group ((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}
{4} matches the previous token exactly 4 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
2nd Capturing Group (25[0-5]|(2[0-4]|1\d|[1-9]|)\d)
1st Alternative 25[0-5]
25 matches the characters 25 literally (case sensitive)
Match a single character present in the list below [0-5]
0-5 matches a single character in the range between 0 (index 48) and 5 (index 53) (case sensitive)
2nd Alternative (2[0-4]|1\d|[1-9]|)\d
3rd Capturing Group (2[0-4]|1\d|[1-9]|)
1st Alternative 2[0-4]
2 matches the character 2 with index 5010 (3216 or 628) literally (case sensitive)
Match a single character present in the list below [0-4]
0-4 matches a single character in the range between 0 (index 48) and 4 (index 52) (case sensitive)
2nd Alternative 1\d
1 matches the character 1 with index 4910 (3116 or 618) literally (case sensitive)
\d matches a digit (equivalent to [0-9])
3rd Alternative [1-9]
Match a single character present in the list below [1-9]
1-9 matches a single character in the range between 1 (index 49) and 9 (index 57) (case sensitive)
4th Alternative  — always finds a zero-length match
\d matches a digit (equivalent to [0-9])
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
$ 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 IP Address Address or not.