I'm trying to create a Regex String with the following rules
- The username is between 4 and 25 characters.
- It must start with a letter.
- It can only contain letters, numbers, and the underscore character.
- It cannot end with an underscore character.
when it meets this criterion I want the output to be true otherwise false, but I only get false for my test cases, here is my code
public class Profile {
public static String username(String str) {
String regularExpression = "^[a-zA-Z][a-zA-Z0-9_](?<=@)\\w+\\b(?!\\_){4,25}$";
if (str.matches(regularExpression)) {
str = "true";
}
else if (!str.matches(regularExpression)) {
str = "false";
}
return str;
}
Main class
Profile profile = new profile();
Scanner s = new Scanner(System.in);
System.out.print(profile.username(s.nextLine()));
input
"aa_"
"u__hello_world123"
output
false
false
Fixed: thanks to everyone who contributed