3

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.

2
  • 1
    You could use the same mechanism like (?=.{3,25}$) Or match \S{3,25} Commented Jun 13, 2020 at 15:34
  • I need to allow numbers and i want to prevent the user from putting only numbers. So any letter must appear at least once upper or lower, and the whole thing must have between 3 and 25 characters Commented Jun 13, 2020 at 15:47

1 Answer 1

3

Username:

Looking at your current patterns it seems like you can use the following pattern to validate your usernames:

^(?=.*[a-zA-Z])\w{3,25}$

See the Online Demo.

  • ^ - Start string ancor.
  • (?=.*[a-zA-Z]) - A positive lookahead, for any character other than newline zero or more times followed by a letter within [a-zA-Z] character class.
  • \w{3,25} - Any character in the class [a-zA-Z0-9_], three times up to a maximum of 25.
  • $ - End string ancor.

enter image description here


Password:

When you have checked for at least a single lower and a single upper case letter, that would defeat the purpose of checking any upper or lower case letter later down the line in your current pattern. Also, . which is any character would match special characters like the ones in your character class [@#$%^&+!?=]. When you don't want these to be mandatory, don't use them in a positive lookahead. Instead maybe try the following:

^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\S{6,}$

The only mandatory characters now are at least one digit, at least one lower case letter and at least one upper case letter. See the Online Demo.

  • ^ - Start string ancor.
  • (?=.*\d) - Positive lookahead for any character other than newline zero or more times followed by a digit.
  • (?=.*[a-z]) - Positive lookahead for any character other than newline zero or more times followed by a lower case letter.
  • (?=.*[A-Z]) - Positive lookahead for any character other than newline zero or more times followed by a upper case letter.
  • \S{6,} - Any non-whitespace character.
  • $ - End string ancor.

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the explanations. is it the \d making at least one digit mandatory ? It's indeed working fine thank you
@TommyD, that is correct. It's shorthand for [0-9], whilst \w is shorthand for [a-zA-Z0-9_]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.