Valid Username Regular Expression

Sort by

recency

|

489 Discussions

|

  • + 1 comment

    I’m tired of this regex challenge. Is it still an interview topic, and how often do you write it at work now that AI can do this quickly?

  • + 0 comments

    It’s a nice challenge to combine regex syntax with problem-solving in Java. Mahadev Book ID Login

  • + 0 comments

    public static final String regularExpression = "^[A-Za-z][A-Za-z0-9_]{7,29}$";`

    "^[A-Za-z]" → first char must be a letter.

    "[A-Za-z0-9_]{7,29}" → remaining chars must be alphanumeric or _, length between 7 and 29.

    Total length = 1 (first char) + [7–29] = 8–30 characters.

    Anchors ^...$ ensure no extra characters sneak in.

  • + 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();
    }
    

    }

  • + 0 comments

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

    public class Solution {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
    
        int n = scanner.nextInt();
        scanner.nextLine();
        for(int i = 0; i<n; i++){
            String word = scanner.nextLine();
            if(word.matches("^[a-zA-Z][a-zA-Z0-9_]{7,29}$")) System.out.println("Valid");
            else System.out.println("Invalid");
        }
    }
    

    }