We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importjava.util.Scanner;importjava.util.regex.Pattern;importjava.util.regex.PatternSyntaxException;/* If a PatternSyntaxException is not thrown by Pattern.compile, then the regular expression is valid.*/publicclassSolution{publicstaticvoidmain(String[]args){Scannerscan=newScanner(System.in);inttestCases=scan.nextInt();scan.nextLine();// gets rid of the pesky newline.while(testCases-->0){Stringpattern=scan.nextLine();try{Pattern.compile(pattern);System.out.println("Valid");}catch(PatternSyntaxExceptionexception){System.out.println("Invalid");}}scan.close();}}
Pattern Syntax Checker
You are viewing a single comment's thread. Return to all comments →
Java solution - passes 100% of test cases
From my HackerRank Java solutions.