Pattern Syntax Checker

Sort by

recency

|

401 Discussions

|

  • + 0 comments

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int testCases = Integer.parseInt(in.nextLine());
    
        while(testCases > 0){
            String pattern = in.nextLine();
              try {
                "".split(pattern);
                System.out.println("Valid");
    
              } catch (Exception e){
                System.out.println("Invalid");
              }
              testCases--;
        }
        in.close();
    }
    
  • + 0 comments

    I love about it is that it helps sharpen both logical thinking and precision, since even a tiny syntax error can make a pattern invalid. Playingexchange ID Login

  • + 0 comments

    It’s also helpful for understanding how powerful (and tricky) regular expressions can be in real-world applications. Playing Exchange

  • + 0 comments

    try { Pattern.compile(pattern); System.out.println("Valid"); } catch (PatternSyntaxException exc) { System.out.println("Invalid"); }

            testCases--;
        }
    
        in.close();
    }
    

    }

  • + 0 comments

    Java 8 solution:

    import java.util.Scanner; import java.util.regex.*;

    public class Solution { public static void main(String[] args){ Scanner in = new Scanner(System.in); int testCases = Integer.parseInt(in.nextLine()); while(testCases>0){ String pattern = in.nextLine(); //Write your code try{ Pattern.compile(pattern); System.out.println("Valid"); } catch(PatternSyntaxException e) { System.out.println("Invalid"); } testCases--; } in.close(); } }