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
CodePudding user response:
You can use
^[a-zA-Z][a-zA-Z0-9_]{3,24}$(?<!_)
^[a-zA-Z]\w{3,24}$(?<!_)
^[a-zA-Z][a-zA-Z0-9_]{2,23}[a-zA-Z0-9]$
^\p{Alpha}[a-zA-Z0-9_]{2,23}\p{Alnum}$
See the regex demo.
Details:
^
- start of string[a-zA-Z]
- an ASCII letter[a-zA-Z0-9_]{3,24}
/\w{3,24}
- three to twenty four ASCII letters, digits or underscores$
- end of string(?<!_)
- a negative lookbehind that makes sure there is no_
(at the end of string).
Note that {3,24}
is used and not {4,25}
because the first [a-zA-Z]
pattern already matches a single char.
Usage:
public static String username(String str) {
return Boolean.toString( str.trim().matches("[a-zA-Z]\\w{3,24}$(?<!_)") );
// return Boolean.toString( str.trim().matches("\\p{Alpha}[a-zA-Z0-9_]{2,23}\\p{Alnum}") );
// return Boolean.toString( str.trim().matches("[a-zA-Z][a-zA-Z0-9_]{2,23}[a-zA-Z0-9]") );
}
See Java demo:
import java.util.*;
import java.util.regex.*;
class Ideone
{
public static String username(String str) {
return Boolean.toString( str.trim().matches("[a-zA-Z]\\w{3,24}$(?<!_)") );
}
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(username("u__hello_world123")); // => true
System.out.println(username("aa_")); // => false
}
}
CodePudding user response:
Sounds like \p{L}
any letter at the start, \w
word characters in middle and \p{Alnum}
in the end.
^\p{L}\w{2,23}\p{Alnum}$
See this demo at regex101 - Thank you @Thefourthbird for providing a Java demo at ideone.com
I'm not sure if it's needed to enable UNICODE_CHARACTER_CLASS
or (?U)
for \w
to support unicode.