-1
^[a-zA-Z0-9](?=.+[a-z])(?=.+[A-Z])(?=.*[0-9])(?=.+[-+_!@#$%^&*.,?])(\\S*[a-zA-Z0-9]){8,}?$

that is my regex for passwords. its wonky and not working all the time, for example : if i try ABcAcs!12 it doesnt work.

this is a task for my homeproject. The password should:

  • have minimum 8 characters,
  • minimum 1 Upper and lower case alphabet,
  • atleast 1 special character
  • the special character is not allowed to be at the start or end of the String.

i have the feeling i need to completely overhaul it, but i hope you can help me.

thobbe
  • 7
  • 1
  • 4
    Does this answer your question? [Regexp Java for password validation](https://stackoverflow.com/questions/3802192/regexp-java-for-password-validation) – Curiosa Globunznik Nov 04 '20 at 00:11
  • 1
    Unless this is an exercise in regex creation, I would just use POJ. I also think it would be easier. – WJS Nov 04 '20 at 00:20

2 Answers2

1
  • The issue is that the pattern starts with matching 1 character in this character class [a-zA-Z0-9].

    At the end of the pattern, this part (\S*[a-zA-Z0-9]){8,} has to match at least 8 times one of [a-zA-Z0-9], and in the string ABcAcs!12 there are 7 of them that match, being ABcAcs and 12

  • Also note that 2 of the lookaheads start with .+ instead of .* meaning they don't take the first following character into account.

    This 9A9a$adfa should be also valid, but due to the .+ it misses the A

  • As you want a miminum of 8, and you already have matched the first character, the quantifier at the end should be {7,}

The final pattern, using negated character classes to prevent backtracking could be

^[a-zA-Z0-9](?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=\D*[0-9])(?=[^-+_!@#$%^&*.,?]*[-+_!@#$%^&*.,?])[-+_!@#$%^&*.,?a-zA-Z0-9]{7,}$

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

Two slightly different approaches:

RegEx [1]

^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[-+_!@#$%^&*.,?])[a-zA-Z\d][a-zA-Z\d\-+_!@#$%^&*.,?]{8,}[a-zA-Z\d]$

^                               // Matches the start of the string
(?=.*[A-Z])                     // Positive lookahead to cheack for presence of a uppercase letter
(?=.*[a-z])                     // Positive lookahead to cheack for presence of an lowercase letter
(?=.*\d)                        // Positive lookahead to cheack for presence of a number
(?=.*[-+_!@#$%^&*.,?])          // Positive lookahead to cheack for presence of a special character letter
[a-zA-Z\d]                      // Matches a "non-special" character at the start of the string
[a-zA-Z\d\-+_!@#$%^&*.,?]{7,}   // Matche any allowed character 7+ times
[a-zA-Z\d]                      // Makes sure the last (minimum 8th) character isn't special
$                               // Matches the end of the string

RegEx [2]

^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=[a-zA-Z\d].*[-+_!@#$%^&*.,?].*[a-zA-Z\d]$)[a-zA-Z\d\-+_!@#$%^&*.,?]{8,}$

^                                               // Matches the start of the string
(?=.*[A-Z])                                     // Positive lookahead to cheack for presence of a uppercase letter
(?=.*[a-z])                                     // Positive lookahead to cheack for presence of an lowercase letter
(?=.*\d)                                        // Positive lookahead to cheack for presence of a number
(?=[a-zA-Z\d].*[-+_!@#$%^&*.,?].*[a-zA-Z\d]$)   // Positive lookahead to check that the string starts and ends with a letter or number but contains a special character
[a-zA-Z\d\-+_!@#$%^&*.,?]{8,}                   // Matches any allowed character 8+ times
$                                               // Matches the end of the string

Output

_Password               [1]         [2]
---------------------------------------
abcdef                   0   =>      0
abcDEF                   0   =>      0
abcDEF789_               0   =>      0
_123DEF789               0   =>      0
abc_DEF123               1   =>      1
abc_D1                   0   =>      0
abc%defghi               0   =>      0
some pass                0   =>      0
Some Other Pass_1234     0   =>      0
alpha_Numeric$12         1   =>      1
Random_1#@!S             1   =>      1
RRARaa???23              1   =>      1
Steven
  • 6,053
  • 2
  • 16
  • 28