Validate IPv6 address using ReGex patterns in C++

Given an IP Address, the task is to validate this IP address and check whether it is IPv6 or not with the help of ReGex(Regular Expression). If the IP Address is valid then print “IPv6 Address” otherwise print “Not”.
A valid IPv4 address is an IP in the form "XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX" where each Xi digit is a hexadecimal digit. For example,
Input-1 −
IP= “3001:0da8:82a3:0:0:8B2E:0270:7224”
Output −
“Not”
Explanation − This is not a valid IPv6 address, return “Not”.
Input-2 −
IP= “2001:0db8:85a3:0000:0000:8a2e:0370:7334”
Output −
“IPv6”
Explanation − This is a valid IPv6 Address, return “IPv6”.
To check whether the given IP address is IPv6 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 −
[Abc]+
Take Input a string specifying a IP address.
Take Input a string specifying a IP address.
A string function validIPAddress(string IP) takes IP address as input and checks whether the input string is valid or not. If it is valid then return “IPv6” otherwise return “Not an IP Address”.
A string function validIPAddress(string IP) takes IP address as input and checks whether the input string is valid or not. If it is valid then return “IPv6” otherwise return “Not an IP Address”.
Creating a regex pattern for the IPv6 address. Since an IPv6 address contains 8 fields in which each field contains the value digits represented as hexadecimal. An IPv6 address looks like XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX separated by ‘colon’.
Creating a regex pattern for the IPv6 address. Since an IPv6 address contains 8 fields in which each field contains the value digits represented as hexadecimal. An IPv6 address looks like XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX separated by ‘colon’.
A valid IPv6 address might be in the range ([0-9a-fA-F]){1,4})\\:){7}([0-9a-fA-F]){1,4}) in which the first digit will be in the ranges from 0-9, second is hexadecimal alphanumeric digit.
A valid IPv6 address might be in the range ([0-9a-fA-F]){1,4})\\:){7}([0-9a-fA-F]){1,4}) in which the first digit will be in the ranges from 0-9, second is hexadecimal alphanumeric digit.
Similarly, for the second field first character would be in the range of 0-9a-fA-F thus the regex pattern would be ‘[0-9a-fA-F]’
Similarly, for the second field first character would be in the range of 0-9a-fA-F thus the regex pattern would be ‘[0-9a-fA-F]’
#include<bits/stdc++.h>
using namespace std;
string validIPAddress(string IP) {
   regex ipv6("((([0-9a-fA-F]){1,4})\\:){7}([0-9a-fA-F]){1,4}");
   if(regex_match(IP, ipv6))
      return "IPv6";
   else
      return "Not";
}
int main(){
   string IP= “3001:0da8:82a3:0:0:8B2E:0270:7224”;
   string ans= validIPAddress(IP);
   cout<<ans<<endl;
   return 0;
}
Running the above code will generate the output as,
Not
Since the input IP Address is not a valid IP address, we will return “Not”.