Valid Username Regular Expression

  • + 0 comments

    import java.util.Scanner;

    public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); // use nextLine() here

        String regex = "^[A-Za-z][A-Za-z0-9_]{7,29}$";
    
        for (int i = 0; i < n; i++) {
            String username = sc.nextLine(); 
    
            if (username.matches(regex)) {
                System.out.println("Valid");
            } else {
                System.out.println("Invalid");
            }
        }
        sc.close();
    }
    

    }