Validate IPv4 address using ReGex patterns in C++

Given an IP Address, the task is to validate this IP address and check whether it is IPv4 or not with the help of ReGex(Regular Expression). If the IP Address is valid then print “IPv4 Address” otherwise print “Not”.

A valid IPv4 address is an IP in the form "X1.X2.X3.X4" where 0 <= Xi <= 255 and Xi cannot contain leading zeros. For example, "" and "" are valid IPv4 addresses but "", while "" and "192.168@1.1" are invalid IPv4 addresses. For example,


IP= “”



Explanation − This is a valid IPv4 address, return “IPv4”.


IP= “312.25.12.1”



Explanation − This is not a valid IPv4 Address, return “No”.

Approach to solve this problem

To check whether the given IP address is IPv4 or not, we use ReGex. A ReGex is an expression that contains a sequence of characters that define a specific pattern. These patterns can be used in algorithms to match the pattern in a string. It is also widely used for Input Validation.

Range Specification − We can specify the characters to make the patterns in the simplest way. To specify the range by using characters, we can use ‘[ ]’ brackets.

Specifying Characters − The above expression indicates an opening bracket and a digit in the range a to z , ‘A’ to ‘Z’, and ‘0’ to ‘9’ as a regex. [a-z], [A-Z] and [0-9].

Repeated Patterns − An expression modifier can be “+” that suggests matching the occurrence of a pattern one or more times or it can be “*” that suggests matching the occurrence of a pattern zero or more times.

The expression [a-z]* will match a blank string.

If you want to specify a group of characters to match one or more times, then you can use the parentheses as follows −


Following is the approach to solving this problem,

  • Take Input a string specifying an IP address.

  • A string function validIPAddress(string IP) takes an IP address as input and checks whether the input string is valid or not. If it is valid then return “IPv4” otherwise return “Not an IP Address”.

  • Creating a regex pattern for the IPv4 address. Since an IPv4 address contains 4 fields in which each field contains the value in the range of 0-255. An IPv4 address looks like XXX.XXX.XXX.XXX.

  • A valid IPv4 address might be in the range (([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0- 5])\.){3} in which the first digit will be in the ranges from 0-9, second 1-9 and third digit in the range of 0-9.

  • Similarly, for the second field first character would be in the range of 100-199, thus the regex pattern would be ‘1[0-9]-0-9]’

  • For the next field, the digit will be in the range 200-249. Thus the regex pattern will be ‘2[0-4][0-9]’ which ensures that the range doesn’t exceed a digit more than 255.

  • The last which is the next field contains digits in the range from 250-255 thus the regex pattern would be 25[0-5].


using namespace std;
string validIPAddress(string IP) {
   regex ipv4("(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0- 9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])");
   if(regex_match(IP, ipv4))
      return "IPv4";
      return "Not";
int main(){
   string IP= “”;
   string ans= validIPAddress(IP);
   return 0;


Running the above code will generate the output as,


Since the input IP Address is a Valid IP address, we will return “IPv4”.

Updated on: 05-Feb-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started