Valid Username Regular Expression

Sort by

recency

|

471 Discussions

|

  • + 0 comments

    import java.util.Scanner;

    public class Solution {

    public static void main(String[] args) {
    
        String REGEX_USER_NAME = "^[a-zA-Z]\\w{7,29}$";
        Scanner in = new Scanner(System.in);
        int numTests = Integer.parseInt(in.nextLine());
    
        while (numTests-- > 0) {
            String username = in.nextLine();
            System.out.println(username.matches(REGEX_USER_NAME) ? "Valid" : "Invalid");
        }
        in.close();
    }
    

    }

  • + 0 comments

    import java.util.Scanner; class UsernameValidator {

    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

    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");
        }           
    }
    

    } }

  • + 1 comment

    Just update the 'regularExpression' with the following expression:

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

    `