Instagram
youtube
Facebook
Twitter

Regular expression to validate an IP address using Java HackerRank solution

Problem

Write a class called MyRegex which will contain a string pattern. You need to write a regular expression and assign it to the pattern such that it can be used to validate an IP address. Use the following definition of an IP address:

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.

Some valid IP address:

000.12.12.034
121.234.12.12
23.45.12.56

Some invalid IP address:

000.12.234.23.23
666.666.23.23
.213.123.23.32
23.45.22.32.
I.Am.not.an.ip

In this problem you will be provided strings containing any combination of ASCII characters. You have to write a regular expression to find the valid IPs.

Just write the MyRegex class which contains a String pattern. The string should contain the correct regular expression.

(MyRegex class MUST NOT be public)

Sample Input

000.12.12.034
121.234.12.12
23.45.12.56
00.12.123.123123.123
122.23
Hello.IP

Sample Output

true
true
true
false
false
false

Solution

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;

class Solution{

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            String IP = in.next();
            System.out.println(IP.matches(new MyRegex().pattern));
        }

    }
}

//Write your code here
class MyRegex
{
    static final String pattern = 
        "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";

    public String getPattern() {
        return pattern;
    }
}

Steps involved in this solution:

1. Import the required packages for working with regular expressions, input, and output.

2. Define the MyRegex class, which will contain the regular expression pattern for validating IP addresses.

3. Inside the MyRegex class, define a static, final, String named pattern. This string holds the regular expression pattern for valid IP addresses, as described in previous responses.

4. In the MyRegex class, create a getter method, getPattern, to access the regular expression pattern.

5. Define the Solution class, which contains the main method and is responsible for reading and testing IP addresses.

6. Inside the main method of the Solution class, create a Scanner object (in) to read input from the standard input.

7. Use a while loop to read IP addresses while there's input (in.hasNext()). For each input IP address, use the matches method to check if it matches the regular expression pattern defined in the MyRegex class. Print "true" if the IP address is valid and "false" if it's not.

8. To execute the program, provide input IP addresses through the standard input (stdin).

9. The program will display "true" or "false" for each input IP address based on whether it matches the regular expression pattern.