DebugPointer
Published on

Regex for IP Address IPV4 validation

Regex for IP Address IPV4 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.

IPv6, or Internet Protocol version 6, is the latest version of the Internet Protocol, the communications protocol that is used for transmitting data over a network. It was developed to address the shortage of available IP addresses that was becoming a problem with the previous version, IPv4.

IPv6 addresses are longer and use a different format than IPv4 addresses. They are 128 bits long and are written in hexadecimal notation, with eight groups of four hexadecimal digits separated by colons, such as 2001:0db8:85a3:0000:0000:8a2e:0370:7334. This allows for a much larger pool of available addresses, which is necessary to support the continued growth of the internet.

IPv6 also includes several other improvements and enhancements over IPv4, including support for more efficient routing, better support for mobile devices, and improved security features. It is gradually being adopted by network operators and internet service providers around the world, but it is not yet widely used.

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 IPV4 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 IP Address IPV4 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 IP Address IPV4 Address is valid or not

Regular Expression-

/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))
$/gm

Test string examples for the above regex-

Input StringMatch Output
3263.6543.31VDdoes not match
1:2:3:4:5:6:7:8matches
32:12:00:23:42:AAdoes not match
0::0:1matches

Here is a detailed explanation of the above regex-

/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))
$/gm

^ asserts position at start of a line
1st Capturing Group (([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))
1st Alternative ([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}
2nd Capturing Group ([0-9a-fA-F]{1,4}:){7,7}
{7,7} matches the previous token exactly 7 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]
{1,4} matches the previous token between 1 and 4 times, as many times as possible, giving back as needed (greedy)
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 5810 (3A16 or 728) literally (case sensitive)
Match a single character present in the list below [0-9a-fA-F]
{1,4} matches the previous token between 1 and 4 times, as many times as possible, giving back as needed (greedy)
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)
2nd Alternative ([0-9a-fA-F]{1,4}:){1,7}:
3rd Capturing Group ([0-9a-fA-F]{1,4}:){1,7}
{1,7} matches the previous token between 1 and 7 times, as many times as possible, giving back as needed (greedy)
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]
{1,4} matches the previous token between 1 and 4 times, as many times as possible, giving back as needed (greedy)
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 5810 (3A16 or 728) literally (case sensitive)
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
3rd Alternative ([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}
4th Capturing Group ([0-9a-fA-F]{1,4}:){1,6}
{1,6} matches the previous token between 1 and 6 times, as many times as possible, giving back as needed (greedy)
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]
{1,4} matches the previous token between 1 and 4 times, as many times as possible, giving back as needed (greedy)
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 5810 (3A16 or 728) literally (case sensitive)
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
Match a single character present in the list below [0-9a-fA-F]
{1,4} matches the previous token between 1 and 4 times, as many times as possible, giving back as needed (greedy)
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)
4th Alternative ([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}
5th Capturing Group ([0-9a-fA-F]{1,4}:){1,5}
{1,5} matches the previous token between 1 and 5 times, as many times as possible, giving back as needed (greedy)
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]
{1,4} matches the previous token between 1 and 4 times, as many times as possible, giving back as needed (greedy)
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 5810 (3A16 or 728) literally (case sensitive)
6th Capturing Group (:[0-9a-fA-F]{1,4}){1,2}
{1,2} matches the previous token between 1 and 2 times, as many times as possible, giving back as needed (greedy)
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
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
Match a single character present in the list below [0-9a-fA-F]
{1,4} matches the previous token between 1 and 4 times, as many times as possible, giving back as needed (greedy)
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)
5th Alternative ([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}
7th Capturing Group ([0-9a-fA-F]{1,4}:){1,4}
8th Capturing Group (:[0-9a-fA-F]{1,4}){1,3}
6th Alternative ([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}
9th Capturing Group ([0-9a-fA-F]{1,4}:){1,3}
10th Capturing Group (:[0-9a-fA-F]{1,4}){1,4}
7th Alternative ([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}
11th Capturing Group ([0-9a-fA-F]{1,4}:){1,2}
12th Capturing Group (:[0-9a-fA-F]{1,4}){1,5}
8th Alternative [0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})
Match a single character present in the list below [0-9a-fA-F]
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
13th Capturing Group ((:[0-9a-fA-F]{1,4}){1,6})
9th Alternative :((:[0-9a-fA-F]{1,4}){1,7}|:)
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
15th Capturing Group ((:[0-9a-fA-F]{1,4}){1,7}|:)
10th Alternative fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}
fe80: matches the characters fe80: literally (case sensitive)
17th Capturing Group (:[0-9a-fA-F]{0,4}){0,4}
% matches the character % with index 3710 (2516 or 458) literally (case sensitive)
Match a single character present in the list below [0-9a-zA-Z]
11th Alternative ::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])
:: matches the characters :: literally (case sensitive)
18th Capturing Group (ffff(:0{1,4}){0,1}:){0,1}
20th Capturing Group ((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}
23rd Capturing Group (25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])
12th Alternative ([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])
25th Capturing Group ([0-9a-fA-F]{1,4}:){1,4}
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
26th Capturing Group ((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}
29th Capturing Group (25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])
$ 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 IPV4 Address or not.