Given an IP address, the task is to validate this IP address with the help of Regex (Regular Expression) in C++ as a valid IPv4 address or IPv6 address. If IP address is not valid then print invalid IP address.
Examples:
Input: str = “203.120.223.13”
Output: Valid IPv4Input: str = “000.12.234.23.23”
Output: Invalid IPInput: str = “2F33:12a0:3Ea0:0302”
Output: Valid IPv6
Input: str = “I.Am.not.an.ip”
Output: Invalid IP
Approach:
- Regex (Regular Expression) In C++ will be used to check the IP address.
-
Range Specifications
Specifying a range of characters or literals is one of the simplest criteria used in a regex.i) [a-z] ii) [A-Za-z0-9]
- In the above expression ([]) square brackets are used to specify the range.
- The first expression will match exactly one lowercase character.
- The second expression specifies the range containing one single uppercase character, one lowercase character, and a digit from 0 to 9.
- Now to include a ‘.’ as part of an expression, we need to escape ‘.’ and this can be done as :
[\\.0-9]
The above expression indicates an ‘.’ and a digit in the range 0 to 9 as a regex.
- regex_match() function is used to match the given pattern. This function returns true if the given expression matches the string. Otherwise, the function returns false.
Here is the implementation of the above approach.
// C++ program to validate // IP address using Regex #include <bits/stdc++.h> using namespace std; // Function for Validating IP string Validate_It(string IP) { // Regex expression for validating IPv4 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])"); // Regex expression for validating IPv6 regex ipv6("((([0-9a-fA-F]){1,4})\\:){7}([0-9a-fA-F]){1,4}"); // Checking if it is a valid IPv4 addresses if (regex_match(IP, ipv4)) return "Valid IPv4"; // Checking if it is a valid IPv6 addresses else if (regex_match(IP, ipv6)) return "Valid IPv6"; // Return Invalid return "Invalid IP"; } // Driver Code int main() { // IP addresses to validate string IP = "203.120.223.13"; cout << Validate_It(IP) << endl; IP = "fffe:3465:efab:23fe:2235:6565:aaab:0001"; cout << Validate_It(IP) << endl; IP = "2F33:12a0:3Ea0:0302"; cout << Validate_It(IP) << endl; return 0; } |
Valid IPv4 Valid IPv6 Invalid IP
Time Complexity : O (N)
Auxiliary Space : O (1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to validate MAC address using Regular Expression
- How to validate an IP address using Regular Expressions in Java
- Program to validate an IP address
- Python program to validate an IP Address
- Difference Between Network Address Translation (NAT) and Port Address Translation (PAT)
- Difference between MAC Address and IP Address
- Extracting each word from a String using Regex in Java
- Print first letter of each word in a string using regex
- Get the first letter of each word in a string using regex in Java
- Find all the patterns of “1(0+)1” in a given string using Python Regex
- How to Remove repetitive characters from words of the given Pandas DataFrame using Regex?
- Java | Removing whitespaces using Regex
- Python | Program that matches a word containing 'g' followed by one or more e's using regex
- Check if a string contains only alphabets in Java using Regex
- The most occurring number in a string using Regex in python
- Replace values in Pandas dataframe using regex
- Count occurrences of a given character using Regex in Java
- Name validation using IGNORECASE in Python Regex
- Python - Substituting patterns in text using regex
- Categorize Password as Strong or Weak using Regex in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

