Skip to content

Commit a792fd2

Browse files
authored
Create PatternSyntaxChecker.java
1 parent 8bc1623 commit a792fd2

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Regex/PatternSyntaxChecker.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Author: Atharv Damle
2+
// Check if a regex (regular expression) is valid.
3+
// Full Question: https://www.hackerrank.com/challenges/pattern-syntax-checker/problem
4+
5+
import java.util.Scanner;
6+
import java.util.regex.*;
7+
8+
public class PatternSyntaxChecker
9+
{
10+
public static void main(String[] args){
11+
12+
Scanner in = new Scanner(System.in);
13+
14+
int testCases = Integer.parseInt(in.nextLine());
15+
while(testCases>0){
16+
17+
// Accept the pattern as input.
18+
String pattern = in.nextLine();
19+
try
20+
{
21+
// Load the pattern. Try to create a Pattern object. If it fails, an error will be raised.
22+
Pattern.compile(pattern);
23+
System.out.println("Valid");
24+
}
25+
catch (Exception e)
26+
{
27+
System.out.println("Invalid");
28+
}
29+
30+
testCases--;
31+
}
32+
}
33+
}
34+
35+
36+

0 commit comments

Comments
 (0)