- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPatternSyntaxChecker.java
More file actions
Latest commit
32 lines (27 loc) · 875 Bytes
/
Copy pathPatternSyntaxChecker.java
File metadata and controls
32 lines (27 loc) · 875 Bytes
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
// https://www.hackerrank.com/challenges/pattern-syntax-checker/problem
importjava.util.Scanner;
importjava.util.regex.Pattern;
importjava.util.regex.PatternSyntaxException;
publicclassPatternSyntaxChecker {
publicstaticvoidmain(String[] args) {
Scannerscanner = newScanner(System.in);
intqueries = scanner.nextInt();
scanner.nextLine();
while (queries-- > 0) {
Stringregex = scanner.nextLine();
if (isValidPattern(regex)) {
System.out.println("Valid");
} else {
System.out.println("Invalid");
}
}
}
privatestaticbooleanisValidPattern(Stringregex) {
try {
Pattern.compile(regex);
returntrue;
} catch (PatternSyntaxExceptionexception) {
returnfalse;
}
}
}