Valid Username Regular Expression

Sort by

recency

|

469 Discussions

|

  • + 0 comments

    public class Solution {

    private static final Scanner scan = new Scanner(System.in);
    
    public static void main(String[] args) {
        int n = Integer.parseInt(scan.nextLine());
        while (n-- != 0) {
            String userName = scan.nextLine();
    
            if(userName.length()>=8 && userName.length()<=30){
                char firstChar=userName.charAt(0);
                if(userName.matches("[a-zA-Z0-9_]*") && Character.isLetter(firstChar)){
                    System.out.println("Valid");
                }else{
                    System.out.println("Invalid");
                }
            }else{
                System.out.println("Invalid");
            }
    }
    

    }}

  • + 0 comments

    class UsernameValidator { /* * Write regular expression here. */ public static final String regularExpression = ("^[a-zA-Z][a-zA-Z0-9_]{7,29}$"); }

    public class Solution { private static final Scanner scan = new Scanner(System.in);

    public static void main(String[] args) { int n = Integer.parseInt(scan.nextLine()); while (n-- != 0) { String userName = scan.nextLine();

        if (userName.matches(UsernameValidator.regularExpression)) {
            System.out.println("Valid");
        } else {
            System.out.println("Invalid");
        }           
    }
    

    } }

  • + 0 comments

    Just update the 'regularExpression' with the following expression:

    public static final String regularExpression ="[a-zA-Z][a-zA-Z0-9_]{7,29}";

    `

  • + 0 comments

    First Case is broken. Second line of the this case is Julia and it is not valid for these rule. but test case is giving wrong output for julia input.

  • + 0 comments

    why is there a compalation error to my solution? do you have it too?

    class UsernameValidator { Scanner scan = new Scanner (System.in);

    for(int i=0;i<scan.nextInt();i++){
        String s=scan.nextLine();
        if(s.length()<8 || s.length()>30)
            System.out.println("Invalid");
        else{
            if(s.matches("^[a-zA-Z_]+$"))
                System.out.println("Valid");
            else
                System.out.println("Invalid");
    
        }
    }