I need the following regex pattern to validate a username and a password :
Username :
- Between 3 and 25 characters
- At least one character [a-zA-Z]
- Any character such as : 0-9, a-z, A-Z, _
i have ended up with this pattern but i can't figure out how to set the {3,25} (my tests are not validating it yet) :
^[a-zA-Z0-9_]*[a-zA-Z][a-zA-Z0-9_]*{3,25}$
Password :
* ^ # start-of-string
* (?=.*[0-9]) # a digit must occur at least once
* (?=.*[a-z]) # a lower case letter must occur at least once
* (?=.*[A-Z]) # an upper case letter must occur at least once
* (?=.*[a-zA-Z]) # any letter upper or lower case
* (?=.*[@#$%^&+=]) # a special character must occur at least once
* (?=\\S+$) # no whitespace allowed in the entire string
* .{6,} # anything, at least six places though
* $ # end-of-string
*/
private Pattern VALID_PASSWORD = Pattern.compile("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z])(?=.*[@#$%^&+!?=])(?=\\S+$).{6,}$");
I just need to change this pattern to allow special characters but i don't want to make them mandatory.


(?=.{3,25}$)Or match\S{3,25}