Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language Java . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Java Regex-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To Java
JAVA was developed by James Gosling at Sun Microsystems Inc in the year 1991, later acquired by Oracle Corporation. It is a simple programming language. Java makes writing, compiling, and debugging programming easy. It helps to create reusable code and modular programs.
Java is a class-based, object-oriented programming language and is designed to have as few implementation dependencies as possible. A general-purpose programming language made for developers to write once run anywhere that is compiled Java code can run on all platforms that support Java. Java applications are compiled to byte code that can run on any Java Virtual Machine. The syntax of Java is similar to c/c++.
Link for the Problem – Java Regex – Hacker Rank Solution
Java Regex – Hacker Rank 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 . 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
Java Regex – Hacker Rank 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)); } } } /* [01]?\\d{1,2} matches numbers 0-199. 2[0-4]\\d matches numbers 200-249 25[0-5] matches numbers 250-255 */ class MyRegex { String num = "([01]?\\d{1,2}|2[0-4]\\d|25[0-5])"; String pattern = num + "." + num + "." + num + "." + num; }
I have been exploring for a bit for any high quality articles or blog posts on this sort of area . Exploring in Yahoo I at last stumbled upon this web site. Reading this info So i’m happy to convey that I have a very good uncanny feeling I discovered exactly what I needed. I most certainly will make sure to don’t forget this web site and give it a look on a constant basis.