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:Ā
Ā
Input: str = "000.12.12.034" Output: True Input: str = "000.12.234.23.23" Output: False Input: str = "121.234.12.12" Output: True Input: str = "I.Am.not.an.ip" Output: False
Ā
Approach:Ā
In this article, the IP address is validated with the help of Regular Expressions or Regex. Below are the steps to solve this problem using ReGex:
Ā
- Get the string.
- Regular expression to validate an IP address:Ā
Ā
// ReGex to numbers from 0 to 255
zeroTo255 -> (\\d{1, 2}|(0|1)\\d{2}|2[0-4]\\d|25[0-5])
// ReGex to validate complete IP address
IPAddress -> zeroTo255 + "\\." + zeroTo255
+ "\\." + zeroTo255
+ "\\." + zeroTo255;
- where:Ā
- \d represents digits in regular expressions, same as [0-9]
- \\d{1, 2} catches any one or two-digit number
- (0|1)\\d{2} catches any three-digit number starting with 0 or 1.
- 2[0-4]\\d catches numbers between 200 and 249.
- 25[0-5] catches numbers between 250 and 255.
- Match the string with the Regex. In Java, this can be done using Pattern.matcher().Ā
Ā - Return true if the string matches with the given regex, else return false.
Below is the implementation of the above approach:
Ā
Java
// Java program to validate an IP address// using Regular Expression or ReGexĀ
import java.util.regex.*;Ā
class IPAddressValidation {Ā
Ā Ā Ā Ā // Function to validate the IPs address.Ā Ā Ā Ā public static boolean isValidIPAddress(String ip)Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Regex for digit from 0 to 255.Ā Ā Ā Ā Ā Ā Ā Ā String zeroTo255Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = "(\\d{1,2}|(0|1)\\"Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + "d{2}|2[0-4]\\d|25[0-5])";Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Regex for a digit from 0 to 255 andĀ Ā Ā Ā Ā Ā Ā Ā // followed by a dot, repeat 4 times.Ā Ā Ā Ā Ā Ā Ā Ā // this is the regex to validate an IP address.Ā Ā Ā Ā Ā Ā Ā Ā String regexĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = zeroTo255 + "\\."Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + zeroTo255 + "\\."Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + zeroTo255 + "\\."Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + zeroTo255;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Compile the ReGexĀ Ā Ā Ā Ā Ā Ā Ā Pattern p = Pattern.compile(regex);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If the IP address is emptyĀ Ā Ā Ā Ā Ā Ā Ā // return falseĀ Ā Ā Ā Ā Ā Ā Ā if (ip == null) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return false;Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Pattern class contains matcher() methodĀ Ā Ā Ā Ā Ā Ā Ā // to find matching between given IP addressĀ Ā Ā Ā Ā Ā Ā Ā // and regular expression.Ā Ā Ā Ā Ā Ā Ā Ā Matcher m = p.matcher(ip);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Return if the IP addressĀ Ā Ā Ā Ā Ā Ā Ā // matched the ReGexĀ Ā Ā Ā Ā Ā Ā Ā return m.matches();Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Driver codeĀ Ā Ā Ā public static void main(String args[])Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā // Checking for True case.Ā Ā Ā Ā Ā Ā Ā Ā // Test Case: 1Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("Test Case 1:");Ā Ā Ā Ā Ā Ā Ā Ā String str1 = "000.12.12.034";Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("Input: " + str1);Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "Output: "Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + isValidIPAddress(str1));Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Test Case: 2Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("\nTest Case 2:");Ā Ā Ā Ā Ā Ā Ā Ā String str2 = "121.234.12.12";Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("Input: " + str2);Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "Output: "Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + isValidIPAddress(str2));Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Checking for False case.Ā Ā Ā Ā Ā Ā Ā Ā // Test Case: 3Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("\nTest Case 3:");Ā Ā Ā Ā Ā Ā Ā Ā String str3 = "000.12.234.23.23";Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("Input: " + str3);Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "Output: "Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + isValidIPAddress(str3));Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Test Case: 4Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("\nTest Case 4:");Ā Ā Ā Ā Ā Ā Ā Ā String str4 = "I.Am.not.an.ip";Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("Input: " + str4);Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "Output: "Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + isValidIPAddress(str4));Ā Ā Ā Ā }} |
Output:Ā
Test Case 1: Input: 000.12.12.034 Output: true Test Case 2: Input: 121.234.12.12 Output: true Test Case 3: Input: 000.12.234.23.23 Output: false Test Case 4: Input: I.Am.not.an.ip Output: false
Ā



