Valid Username Regular Expression

  • + 0 comments

    JAVA SOLUTION

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

    public class Solution {

    public static void validator(String word){
    
        // First character must be a letter : ^[a-zA-Z]
    
        // Following characters can be either letters , numbers or _ : [a-zA-Z_0-9]
    
        // Must have 8-30 characters so after the first validation to the last character 
        // can have 7 to 29 =  {7,29}$
    
        if (word.matches("^[a-zA-Z][a-zA-Z_0-9]{7,29}$")) {
            System.out.println("Valid");
        }
        else{
           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 numWords = sc.nextInt();
    
        sc.nextLine();
    
        for (int i = 0; i < numWords; i++) {
            String word = sc.nextLine();
            validator(word);
    
        }
    
        sc.close();
    
    
    }
    

    }