How to validate an IP address using ReGex
Last Updated :
15 Feb, 2023
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++
// 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 = "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
// Java program to validate
// IP address using Regex
import java.util.regex.*;
class GFG {
// Function for Validating IP
static String Validate_It(String IP)
{
// Regex expression for validating IPv4
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])";
// Regex expression for validating IPv6
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);
// Checking if it is a valid IPv4 addresses
if (p.matcher(IP).matches())
return "Valid IPv4";
// Checking if it is a valid IPv6 addresses
else if (p1.matcher(IP).matches())
return "Valid IPv6";
// Return Invalid
return "Invalid IP";
}
// Driver Code
public static void main(String args[])
{
// IP addresses to validate
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));
}
}
// This code is contributed by Aman Kumar.
Python3
# Python3 program to validate
# IP address using Regex
import re
# Function for Validating IP
def Validate_It(IP):
# Regex expression for validating IPv4
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])$"
# Regex expression for validating IPv6
regex1 = "((([0-9a-fA-F]){1,4})\\:){7}"\
"([0-9a-fA-F]){1,4}"
p = re.compile(regex)
p1 = re.compile(regex1)
# Checking if it is a valid IPv4 addresses
if (re.search(p, IP)):
return "Valid IPv4"
# Checking if it is a valid IPv6 addresses
elif (re.search(p1, IP)):
return "Valid IPv6"
# Return Invalid
return "Invalid IP"
# Driver Code
# IP addresses to validate
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))
# This code is contributed by avanitrachhadiya2155
C#
// C# program to validate
// IP address using Regex
using System;
using System.Text.RegularExpressions;
class GFG {
// Function for Validating IP
static string Validate_It(string IP)
{
// Regex expression for validating IPv4
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])";
// Regex expression for validating IPv6
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);
// Checking if it is a valid IPv4 addresses
if (p.IsMatch(IP))
return "Valid IPv4";
// Checking if it is a valid IPv6 addresses
else if (p1.IsMatch(IP))
return "Valid IPv6";
// Return Invalid
return "Invalid IP";
}
// Driver Code
public static void Main()
{
// IP addresses to validate
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));
}
}
// This code is contributed by Pushpesh Raj.
JavaScript
// JavaScript program to validate
// IP address using Regex
// Function for Validating IP
function Validate_It(IP) {
// Regex expression for validating IPv4
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])/;
// Regex expression for validating IPv6
let ipv6 = /((([0-9a-fA-F]){1,4})\:){7}([0-9a-fA-F]){1,4}/;
// Checking if it is a valid IPv4 addresses
if (IP.match(ipv4))
return "Valid IPv4";
// Checking if it is a valid IPv6 addresses
else if (IP.match(ipv6))
return "Valid IPv6";
// Return Invalid
return "Invalid IP";
}
// Driver Code
function main() {
// IP addresses to validate
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();
// This code is contributed by akashish__
OutputInvalid IP
Valid IPv6
Invalid IP
Time Complexity: O (N)
Auxiliary Space: O (1)
Similar Reads
How to validate MAC address using Regular Expression Given string str, the task is to check whether the given string is a valid MAC address or not by using Regular Expression. A valid MAC address must satisfy the following conditions: It must contain 12 hexadecimal digits.One way to represent them is to form six pairs of the characters separated with
6 min read
Program to validate an IP address Write a program to Validate an IP Address. An IP address is a unique identifier for devices on a network, enabling internet communication. It has two versions: IPv4 and IPv6. We will validate IPv4 and IPv6 addresses separately.Table of ContentIPv4 Addresses ValidationIPv6 Addresses ValidationIPv4 Ad
15+ min read
How to validate ISIN using Regular Expressions ISIN stands for International Securities Identification Number. Given string str, the task is to check whether the given string is a valid ISIN(International Securities Identification Number) or not by using Regular Expression. The valid ISIN(International Securities Identification Number) must sati
6 min read
How to validate an IP address using Regular Expressions in Java Given an IP address, the task is to validate this IP address with the help of Regular Expressions.The IP address is a string in the form "A.B.C.D", where the value of A, B, C, and D may range from 0 to 255. Leading zeros are allowed. The length of A, B, C, or D can't be greater than 3.Examples: Inpu
3 min read
How to Validate MICR Code using Regular Expression? MICR stands for Magnetic Ink Character Recognition. This technology provides transaction security, ensuring the correctness of bank cheques. MICR code makes cheque processing faster and safer. MICR Technology reduces cheque-related fraudulent activities. Structure of a Magnetic Ink Character Recogni
5 min read