How to validate an IP address using Regular Expressions in Java Last Updated : 10 Jun, 2021 Comments Improve Suggest changes Like Article Like Report 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 Comment More infoAdvertise with us Next Article How to validate an IP address using Regular Expressions in Java prashant_srivastava Follow Improve Article Tags : Java Technical Scripter Computer Networks DSA Technical Scripter 2019 java-regular-expression +2 More Practice Tags : Java 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 How to validate a Username using Regular Expressions in Java Given a string str which represents a username, the task is to validate this username with the help of Regular Expressions. A username is considered valid if all the following constraints are satisfied: The username consists of 6 to 30 characters inclusive. If the username consists of less than 6 or 3 min read How to Validate Email Address without using Regular Expression in JavaScript ? Email validation in JavaScript is the process of ensuring that an email address entered by the user is in the correct format and is a valid email address or not. This is typically done on the client side using JavaScript before the form is submitted to the server.An email address must have the follo 5 min read How to validate a domain name using Regular Expression Given string str, the task is to check whether the given string is a valid domain name or not by using Regular Expression.The valid domain name must satisfy the following conditions: The domain name should be a-z or A-Z or 0-9 and hyphen (-).The domain name should be between 1 and 63 characters long 6 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 pin code of India using Regular Expression Given a string of positive number ranging from 0 to 9, the task is to check whether the number is valid pin code or not by using a Regular Expression. The valid pin code of India must satisfy the following conditions. It can be only six digits.It should not start with zero.First digit of the pin cod 6 min read 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.23 5 min read How to validate Indian driving license number using Regular Expression Given string str, the task is to check whether the given string is a valid Indian driving license number or not by using Regular Expression.The valid Indian driving license number must satisfy the following conditions: It should be 16 characters long (including space or hyphen (-)).The driving licen 7 min read How to validate Indian Passport number using Regular Expression Given a string str of alphanumeric characters, the task is to check whether the given string is a valid passport number or not by using Regular Expression. A valid passport number in India must satisfy the following conditions: It should be eight characters long.The first character should be an uppe 5 min read Like