You are viewing a single comment's thread. Return to all comments →
Solution for this problem:
import java.util.Scanner; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException;
public class Solution {
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int testCases = scanner.nextInt(); scanner.nextLine(); // consume newline for (int i = 0; i < testCases; i++) { String pattern = scanner.nextLine(); if (isValidRegex(pattern)) { System.out.println("Valid"); } else { System.out.println("Invalid"); } } scanner.close(); } static boolean isValidRegex(String pattern) { try { // Try compiling the pattern Pattern.compile(pattern); return true; // If no exception is thrown, pattern is valid } catch (PatternSyntaxException e) { return false; // Pattern syntax is invalid } }
}
Seems like cookies are disabled on this browser, please enable them to open this website
Pattern Syntax Checker
You are viewing a single comment's thread. Return to all comments →
Solution for this problem:
import java.util.Scanner; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException;
public class Solution {
}