Skip to content

Commit 74d3e85

Browse files
authored
Using pattern and matcher
using [] and (), |
1 parent 0137c73 commit 74d3e85

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

RegexProblem.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
https://www.hackerrank.com/challenges/java-regex/problem
3+
*/
4+
import java.util.regex.Matcher;
5+
import java.util.regex.Pattern;
6+
import java.util.Scanner;
7+
8+
class Solution{
9+
10+
public static void main(String[] args){
11+
Scanner in = new Scanner(System.in);
12+
while(in.hasNext()){
13+
String IP = in.next();
14+
System.out.println(IP.matches(new MyRegex().pattern));
15+
}
16+
17+
}
18+
}
19+
20+
//Write your code here
21+
class MyRegex{
22+
23+
/*
24+
matcher-- used to search the pattern
25+
pattern- to be used in search
26+
0-9--> [0-9] // [0-9] any value range from 0 to 9
27+
09-99 --> [0-9][0-9] ......bocz leading zero is allowed
28+
099-199 --> (0|1)[0-9][0-9] // (0|1) choose either 0 or 1
29+
200-249--> 2 [0-4][0-9]
30+
250-254 --> 25 [0-4]
31+
*/
32+
String value="([0-9]|[0-9][0-9]|(0|1)[0-9][0-9]|2[0-4][0-9]|25[0-5])";
33+
34+
String pattern=value+"."+value+"."+value+"."+value;
35+
36+
}

0 commit comments

Comments
 (0)