-
Notifications
You must be signed in to change notification settings - Fork 1
/
RegexProblem.java
36 lines (29 loc) · 956 Bytes
/
RegexProblem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
https://www.hackerrank.com/challenges/java-regex/problem
*/
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{
/*
matcher-- used to search the pattern
pattern- to be used in search
0-9--> [0-9] // [0-9] any value range from 0 to 9
09-99 --> [0-9][0-9] ......bocz leading zero is allowed
099-199 --> (0|1)[0-9][0-9] // (0|1) choose either 0 or 1
200-249--> 2 [0-4][0-9]
250-254 --> 25 [0-4]
*/
String value="([0-9]|[0-9][0-9]|(0|1)[0-9][0-9]|2[0-4][0-9]|25[0-5])";
String pattern=value+"."+value+"."+value+"."+value;
}