-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatternSyntaxChecker.java
More file actions
44 lines (32 loc) · 1 KB
/
Copy pathPatternSyntaxChecker.java
File metadata and controls
44 lines (32 loc) · 1 KB
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
37
38
39
40
41
42
43
44
/*Given a regex pattern, check whether its syntax is correct.
If Pattern.compile(pattern) works → Print Valid
If it throws an exception → Print Invalid
Example
Input:([A-Z])(.+)
Output:Valid
Because the regex syntax is correct. ✅
Input:[AZ[a-z](a-z)
Output:Invalid*/
import java.util.Scanner;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// Read number of test cases
int testCases = Integer.parseInt(in.nextLine());
while (testCases-- > 0) {
// Read regex pattern
String pattern = in.nextLine();
try {
// Try compiling the regex
Pattern.compile(pattern);
// If no exception occurs
System.out.println("Valid");
} catch (Exception e) {
// If regex syntax is wrong
System.out.println("Invalid");
}
}
in.close();
}
}