2

I have already tried several options on this topic here on Stackoverflow, but none of them worked.

I have a database of passwords that I need to review for compliance.

I figured how to build an expression to match the passwords that are compliant with the required complexity:

8-32 characters letters numbers special characters

^(?=.*[a-z])(?=.*[A-Z])(?=.*[[:digit:]])(?=.*[[:punct:]]).{8,32}

Now, all I need to do is to get the negative of the above expression to find the password that do not match the required complexity.

I tried to change the expression to this:

(?!^(?=.{8,32}$)(?=.*[[:alpha:]])(?=.*[[:digit:]])(?=.*[[:punct:]])).*

but that does not work.

Thanks for your help

Bluz
  • 5,980
  • 11
  • 32
  • 40

2 Answers2

1

You can reverse your assertion using negative lookahead with alternations like this:

^(?:(?!.*[a-z])|(?!.*[A-Z])|(?!.*[[:digit:]])|(?!.*[[:punct:]])|(?!.{8,32}$)).*

RegEx Demo

RegEx Details:

  • ^: Start
  • (?:: Start non-capture group
    • (?!.*[a-z]): we don't have a lowercase letter ahead
    • |: OR
    • (?!.*[A-Z]): we don't have a uppercase letter ahead
    • |: OR
    • (?!.*[[:digit:]]): we don't have a digit ahead
    • |: OR
    • (?!.*[[:punct:]]): we don't have a punctuation character ahead
    • |: OR
    • (?!.{8,32}$): we don't have 8 to 32 characters ahead
  • ):
  • .*
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Fantastic! Thanks very much @anubhava, this one seems to work. However, interestingly enough, it matches passwords such as "KJ+&)1O{v^R+q}" as negative pattern? I wonder if it's because arithmetic symbols do not count as :punct: ? - I'll give that a try and post back here. – Bluz Apr 23 '21 at 12:24
  • ok I have just discovered fire hre but obviously, characters such as percentage,plus,minus,british pound sign, dollar, etc... do not seem to have a class of their own... So, the expression I was looking for, with anubhava's help, was : /^(?:(?!.*[a-z])|(?!.*[A-Z])|(?!.*[[:digit:]])|(?!.*[[:punct:]])|(?!.{8,32}$)|(?!.*[\`\-\=\[\]\;\'\#\,\.\/\\\¬\!\"\£\$\%\^\&\*\(\)\_\+\{\}\:\@\~\<\>\?\|])).* – Bluz Apr 23 '21 at 12:47
  • `KJ+&)1O{v^R+q}` is a valid password that's why it is not matched by above regex. Do you want to treat it as an invalid case? – anubhava Apr 23 '21 at 13:43
  • Also please understand that `% + - $ # !` etc are all matched as punctuation symbols only. – anubhava Apr 23 '21 at 13:51
0

This regex will highlight bad passwords that are shorter than 8 characters, don't have special characters, upper/lower case letters and numbers:

/^(?:(?!.*[a-z])|(?!.*[A-Z])|(?!.*[[:digit:]])|(?!.*[[:punct:]])|(?!.{8,32}$)|(?!.*[\`\-\=\[\]\;\'\#\,\.\/\\\¬\!\"\£\$\%\^\&\*\(\)\_\+\{\}\:\@\~\<\>\?\|])).*
Bluz
  • 5,980
  • 11
  • 32
  • 40