Skip to content

Commit 2f7a30e

Browse files
committed
Check if a username is correct using Regex
1 parent ce25c57 commit 2f7a30e

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Java/Strings/UserName.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Regular expressions (regexpregexp) help us match or search for patterns in strings. In this problem, you will be given a username. Your task is to check whether that username is valid. A valid username will have the following properties:
3+
1. The username can contain alphanumeric characters and/or underscores(_).
4+
2. The username must start with an alphabetic character.
5+
8 ≤≤ |Username| ≤≤ 30.
6+
To simplify your task, we have provided a portion of the code in the editor. You just need to write down the regexregex pattern which will be used to validate the username input.
7+
8+
Input Format
9+
The first line of input contains an integer NN, representing the number of testcases. Each of the next NN lines contains a string that represents a username.
10+
Constraints
11+
The username consists of any printable characters.
12+
1≤N≤1001≤N≤100
13+
Output Format
14+
For each username, output Valid if the username is valid; otherwise, output Invalid.
15+
*/
16+
17+
import java.io.*;
18+
import java.util.*;
19+
import java.text.*;
20+
import java.math.*;
21+
import java.util.regex.*;
22+
23+
public class UserName {
24+
public static void main(String[] args){
25+
Scanner in = new Scanner(System.in);
26+
int testCases = Integer.parseInt(in.nextLine());
27+
while(testCases>0){
28+
String username = in.nextLine();
29+
String pattern = "^[a-zA-Z]{1,1}[a-zA-Z0-9_]{7,29}$";
30+
Pattern r = Pattern.compile(pattern);
31+
Matcher m = r.matcher(username);
32+
if (m.find( )) {
33+
System.out.println("Valid");
34+
} else {
35+
System.out.println("Invalid");
36+
}
37+
testCases--;
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)