How to validate an IP address using ReGex
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 the IP address is not valid then print an invalid IP address.
Examples:
Input: str = “203.120.223.13”
Output: Valid IPv4
Input: str = “000.12.234.23.23”
Output: Invalid IP
Input: str = “2F33:12a0:3Ea0:0302”
Output: Invalid IP
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++
#include <bits/stdc++.h>
using namespace std;
string Validate_It(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])" );
regex ipv6( "((([0-9a-fA-F]){1,4})\\:){7}([0-9a-fA-F]){1,4}" );
if (regex_match(IP, ipv4))
return "Valid IPv4" ;
else if (regex_match(IP, ipv6))
return "Valid IPv6" ;
return "Invalid IP" ;
}
int main()
{
string IP = "257.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;
}
|
Java
import java.util.regex.*;
class GFG {
static String Validate_It(String IP)
{
String regex= "(([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])" ;
String regex1= "((([0-9a-fA-F]){1,4})\\:){7}([0-9a-fA-F]){1,4}" ;
Pattern p = Pattern.compile(regex);
Pattern p1 = Pattern.compile(regex1);
if (p.matcher(IP).matches())
return "Valid IPv4" ;
else if (p1.matcher(IP).matches())
return "Valid IPv6" ;
return "Invalid IP" ;
}
public static void main(String args[])
{
String IP = "257.120.223.13" ;
System.out.println(Validate_It(IP));
IP = "fffe:3465:efab:23fe:2235:6565:aaab:0001" ;
System.out.println(Validate_It(IP));
IP = "2F33:12a0:3Ea0:0302" ;
System.out.println(Validate_It(IP));
}
}
|
Python3
import re
def Validate_It(IP):
regex = "^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$"
regex1 = "((([0-9a-fA-F]){1,4})\\:){7}" \
"([0-9a-fA-F]){1,4}"
p = re. compile (regex)
p1 = re. compile (regex1)
if (re.search(p, IP)):
return "Valid IPv4"
elif (re.search(p1, IP)):
return "Valid IPv6"
return "Invalid IP"
IP = "257.120.223.13"
print (Validate_It(IP))
IP = "fffe:3465:efab:23fe:2235:6565:aaab:0001"
print (Validate_It(IP))
IP = "2F33:12a0:3Ea0:0302"
print (Validate_It(IP))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG {
static string Validate_It( string IP)
{
string regex= "(([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])" ;
string regex1= "((([0-9a-fA-F]){1,4})\\:){7}([0-9a-fA-F]){1,4}" ;
Regex p = new Regex(regex);
Regex p1 = new Regex(regex1);
if (p.IsMatch(IP))
return "Valid IPv4" ;
else if (p1.IsMatch(IP))
return "Valid IPv6" ;
return "Invalid IP" ;
}
public static void Main()
{
string IP = "257.120.223.13" ;
Console.WriteLine(Validate_It(IP));
IP = "fffe:3465:efab:23fe:2235:6565:aaab:0001" ;
Console.WriteLine(Validate_It(IP));
IP = "2F33:12a0:3Ea0:0302" ;
Console.WriteLine(Validate_It(IP));
}
}
|
Javascript
function Validate_It(IP) {
let 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])/;
let ipv6 = /((([0-9a-fA-F]){1,4})\:){7}([0-9a-fA-F]){1,4}/;
if (IP.match(ipv4))
return "Valid IPv4" ;
else if (IP.match(ipv6))
return "Valid IPv6" ;
return "Invalid IP" ;
}
function main() {
let IP = "257.120.223.13" ;
console.log(Validate_It(IP));
IP = "fffe:3465:efab:23fe:2235:6565:aaab:0001" ;
console.log(Validate_It(IP));
IP = "2F33:12a0:3Ea0:0302" ;
console.log(Validate_It(IP));
}
main();
|
Output
Invalid IP
Valid IPv6
Invalid IP
Time Complexity: O (N)
Auxiliary Space: O (1)
Last Updated :
15 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...