Java Regex - HackerRank Solution with Explanation

Java Regex - Hacker Rank Solution

Hello Friends, How are you? Today I am going to solve the HackerRank Java Regex Problem with a very easy explanation. This is the 21st problem of Java on HackerRank. In this article, you will get more than one approach to solve this problem. So let's start-

{tocify} $title={Table of Contents}

Java Regex - Hacker Rank Solution

HackerRank Java Regex - Problem Statement

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. {codeBox}

Some Valid IP Addresses:

000.12.12.034 121.234.12.12 23.45.12.56 {codeBox}

Some Invalid IP Addresses:

000.12.234.23.23 666.666.23.23 .213.123.23.32 23.45.22.32. I.Am.not.an.ip {codeBox}

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 {codeBox}

Sample Output

true true true false false false {codeBox}

Java Regex - Hacker Rank Solution

Approach I:


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));
        }

    }
}


class MyRegex {
    String num = "([01]?\\d{1,2}|2[0-4]\\d|25[0-5])";
    String pattern = num + "." +  num + "." +  num + "." + num;
}


Approach II:

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


class MyRegex {
	static String zeroTo255 = "(\\d{1,2}|(0|1)\\d{2}|2[0-4]\\d|25[0-5])";
	public static String pattern = zeroTo255 + "\\." + zeroTo255 + "\\." + zeroTo255 + "\\." + zeroTo255;

}

public class JavaRegex {
	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));
		}
		in.close();
	}
}

Also Read:

Disclaimer: The above Problem (Java Regex) is generated by Hackerrank but the Solution is Provided by MyeduWaves. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the contact form.

I hope you have understood the solution to this HackerRank Problem. All these solutions will pass all the test cases. Now visit Java Regex HackerRank Problem and try to solve it again.

All the Best!

Post a Comment

Previous Post Next Post