Pattern Syntax Checker

Sort by

recency

|

393 Discussions

|

  • + 0 comments

    Java 8

    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 p = Pattern.compile(pattern);
                    System.out.println("Valid");
                }catch(Exception e){
                    System.out.println("Invalid");
                }
                testCases--;
                
    		}
    	}
    }
    
  • + 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();
              	//Write your code
                try{
                Pattern pattern2 = Pattern.compile(pattern);
                System.out.println("Valid");
                }catch (PatternSyntaxException e) {  
                    System.out.println("Invalid");
                }
                testCases--; 
    		}
    	}
    
  • + 0 comments

    public class Solution { public static void main(String[] args){

        Scanner in = new Scanner(System.in);
        int testCases = Integer.parseInt(in.nextLine());
    
        while(testCases-->0){
            try {  
            Pattern.compile(in.nextLine()); 
            System.out.println("Valid");
    
        } catch (PatternSyntaxException e) {
            System.out.println("Invalid");
        }
        }
        in.close();
        }
    

    }

  • + 0 comments

    JAVA SOLUTION

    import java.io.; import java.util.;

    public class Solution { public static void pattern(String palabra){ try{ palabra.matches(palabra); System.out.println("Valid"); } catch (Exception e){ System.out.println("Invalid"); } }
    public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */

        Scanner sc = new Scanner(System.in);
        int numero = sc.nextInt();
        sc.nextLine();
    
        for (int i = 0; i < numero; i++) {
            String palabra = sc.nextLine();            
            pattern(palabra);
    
        }                
    }
    

    }

  • + 0 comments

    better approach rather than consuming empty line using nextLine()

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