Java IP Address (IPv4) Regex Examples10 Sept 2024 | 3 min read IP address are the unique numerical identifiers of each devices connected to a network. The first version of the IP addresses is a 32-bit address separated by period (.). In Java, Regex or Regular Expression is an API that defines the string patterns. It is widely used for searching, validating, and manipulating string and passwords. In this section, we will learn how to validate IP address with regular expression (regex). IP Address Validation Rules
ExampleInput: str = "172.16.254.1" Output: True Input: str = "000.123.12.23.28" Output: False Input: str = "I.Am.not.an.ip" Output: False Regular Expression for IP AddressGet the String. Create a regular expression to validate an IP address. // regular expression to validate numbers from 0 to 255 zeroTo255 -> (\\d{1, 2}|(0|1)\\d{2}|2[0-4]\\d|25[0-5]) //regular expression to validate complete IP address IPAddress -> zeroTo255 + "\\." + zeroTo255 + "\\." + zeroTo255 + "\\." + zeroTo255;
In order to match the pattern Java Pattern class provides the matcher() function. The method creates a patten that matches he given input against this pattern. It returns true if the string matches with the given regex, else return false. Syntax:Note: There may be more than one regular expression to validate IP addresses.Some Other Regular Expression for IP Addresses\b(([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.(2[0-5][0-5]|1[0-9][0-9]|[1-9][0-9]|[1-9]))\b ^(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$ Let's create a Java program that validates the IP addresses. ValidateIPAddress.java Output: Input: 172.16.254.1 Output: true Input: 111.234.162.100 Output: true Input: 000.129.24.231.89 Output: false Input: ab.cd.nef.gh.jk Output: false Next TopicConverting Long to Date in JAVA |
Given two arrays, A[] and B[], such that each array contains N and M integers particularly. Our task is to find out the count pairings (A[i], B[j]), which ensure that the product of their count of distinct prime factors is even. Example 1: Input: int arr_A[] = {1, 7} int...
6 min read
In Java, to use the latest version provides some new features added in the latest version. It removes outdated features. The updated Java version contains important enhancements to improve the performance, stability, security of Java applications. Installing the latest version of Java ensures that Java application...
2 min read
Finding the greatest number of values that may be chosen from the array so that the total number of 1s in their binary representation is at most K is the task when an array of integers nums[] and a positive integer K is given. Example 1: Input: int...
3 min read
? Java, with its strong typing system, ensures type safety and ents many common programming errors. However, this also means that you might encounter "incompatible types" errors during compilation. These errors occur when you try to assign or use a value of one type where another type...
4 min read
Introduction: Programmers frequently get into scenarios where they must determine whether a given string contains all digits from 0 to 9. It will be helpful in various situations, including input validation, data validation, and password validation. Problem Statement: Write a Java program that checks if a given string consists...
6 min read
In programming, indentation is just like formatting. It is used to make the code readable to other users because it makes the code easier to edit, displays how the braces match up, and shows the logic of the program in well-organized fashion. It signals to the...
4 min read
The Steps by Knight problem is an example of the graph traversal problem for which the BFS algorithm is utilized. The problem is typically described as follows. Problem Statement A knight is occupied a certain initial position on the chessboard, which is denoted as coordinates x, y. The...
5 min read
ArrayList is a class of Java Collection framework. It uses a dynamic array for storing the objects. It is much similar to Array, but there is no size limit in it. We can add or remove the elements whenever we want. We can store the...
8 min read
In this section, we will discuss what is pernicious number and also create Java programs to check if the given number is a pernicious number or not. The pernicious number program frequently asked in Java coding interviews and academics. Pernicious Number If the total number of 1's in...
4 min read
? In Java, garbage collection is the process of managing memory, automatically. It finds the unused objects (that are no longer used by the program) and delete or remove them to free up the memory. The garbage collection mechanism uses several GC algorithms. The most popular algorithm...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India