HackerRank Java- Pattern Syntax Checker




Using Regex, we can easily match or search for patterns in a text. Before searching for a pattern, we have to specify one using some well-defined syntax.

In this problem, you are given a pattern. You have to check whether the syntax of the given pattern is valid.



  1. import java.util.Scanner;
  2. import java.util.regex.Pattern;
  3. import java.util.regex.PatternSyntaxException;
  4. public class Solution {
  5. public static void main(String[] args) {
  6. Scanner scan = new Scanner(System.in);
  7. int testCases = scan.nextInt();
  8. scan.nextLine();
  9. while (testCases-- > 0) {
  10. String pattern = scan.nextLine();
  11. try {
  12. Pattern.compile(pattern);
  13. System.out.println("Valid");
  14. } catch (PatternSyntaxException exception) {
  15. System.out.println("Invalid");
  16. }
  17. }
  18. scan.close();
  19. }
  20. }
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]


donate a cup of tea :)


Join Our Facebook Group

Share this solution






codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube