Valid Username Regular Expression – Hacker Rank Solution

Valid Username Regular Expression - Hacker Rank Solution
Valid Username Regular Expression- Hacker Rank Solution

Objective

You are updating the username policy on your company’s internal networking platform. According to the policy, a username is considered valid if all the following constraints are satisfied:

  • The username consists of  to  characters inclusive. If the username consists of less than 8 or greater than 30 characters, then it is an invalid username.

Problem Statement: Click Here



Valid Username Regular Expression – Hacker Rank Solution

import java.util.Scanner;

class UsernameValidator {
    public static final String regularExpression = "^[a-zA-Z][\\w]{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");
        }
    }
}

Disclaimer: The above Problem (Valid Username Regular Expression ) is generated by Hackerrank but the Solution is Provided by Chase2Learn. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the following contact form thank you.

Leave a Comment

Your email address will not be published. Required fields are marked *