[HackerRank] Java Syntax Checker
https://www.hackerrank.com/challenges/pattern-syntax-checker/problem?isFullScreen=true
compile()
Compiles the given regular expression into a pattern.
Parameters:
regex - The expression to be compiled
Throws:
PatternSyntaxException - If the expressionโs syntax is invalid
Solution
import java.io.*;
import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int testCases = Integer.parseInt(bufferedReader.readLine());
while (testCases > 0) {
String pattern = bufferedReader.readLine();
try {
Pattern.compile(pattern);
System.out.println("Valid");
} catch (PatternSyntaxException e) {
System.out.println("Invalid");
}
testCases--;
}
}
}
Leave a comment