HackerRank/JAVA

Valid Username Regular Expression

예쁜꽃이피었으면 2024. 1. 19. 09:29

 

 

https://www.hackerrank.com/challenges/valid-username-checker/problem?isFullScreen=true

 

Valid Username Regular Expression | HackerRank

Determine if a username is valid using the power of regex!

www.hackerrank.com

//글자수 8~30

//영어로 시작, 숫자로 시작하면 안됨

//특수문자 안됨.

    

1차) 

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

input값 중에 Samantha_21 가 Valid로 나와야 함.

 

조건 하나 놓쳤었음.  특수문자는 _ (언더바만 허용)

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

 

 

import java.util.Scanner;
class UsernameValidator {
    /*
     * Write regular expression here.
     */
    // public static final String regularExpression = null;
    // public static final String regularExpression = "^[A-Za-z]([A-Za-z0-9]{7,29})$";
    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");
            }           
        }
    }
}

 


 

 

 

더보기

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  or greater than  characters, then it is an invalid username.
  • The username can only contain alphanumeric characters and underscores (_). Alphanumeric characters describe the character set consisting of lowercase characters , uppercase characters , and digits .
  • The first character of the username must be an alphabetic character, i.e., either lowercase character  or uppercase character .

For example:

UsernameValidity
  INVALID; Username length < 8 characters
  VALID
  VALID
  INVALID; Username begins with non-alphabetic character
  INVALID; '?' character not allowed

Update the value of regularExpression field in the UsernameValidator class so that the regular expression only matches with valid usernames.

Input Format

The first line of input contains an integer , describing the total number of usernames. Each of the next  lines contains a string describing the username. The locked stub code reads the inputs and validates the username.

Constraints

  •  
  • The username consists of any printable characters.

Output Format

For each of the usernames, the locked stub code prints Valid if the username is valid; otherwise Invalid each on a new line.

Sample Input 0

8
Julia
Samantha
Samantha_21
1Samantha
Samantha?10_2A
JuliaZ007
Julia@007
_Julia007

Sample Output 0

Invalid
Valid
Valid
Invalid
Invalid
Valid
Invalid
Invalid

Explanation 0

Refer diagram in the challenge statement.

반응형

'HackerRank > JAVA' 카테고리의 다른 글

Java Exception Handling (Try-catch)  (0) 2024.01.22
Java Primality Test  (0) 2024.01.22
[작성중]Java Regex 2 - Duplicate Words  (0) 2024.01.19
[못품]Java Regex  (0) 2024.01.17
java String Tokens  (0) 2024.01.17