A regular expression is required to test at least 3 uppercase, 3 lowercase, 3 digits and 3 special characters

A regular expression is required to check at least 3 uppercase, 3 lowercase, 3 digits and 3 special characters anywhere on the line.

I tried /^(?=.*[^A-Za-z0-9]{3,})(?=.*[AZ]{3,})(?=.*\d{3,})(?=.*[0-9]{3,}).+/ But this check of a continuous line is like :: abcABC123(*) , but did not check: 1a(2b)AB*3cC

+4
source share
3 answers
 /^(?=(.*[^A-Za-z0-9]){3,})(?=(.*[AZ]){3,})(?=(.*\d){3,})(?=.*[az]){3,}).+/ 

So you were close.

+3
source

You were close: you need to copy .* With character classes in your face forward:

 ^(?=(.*[^A-Za-z0-9]){3})(?=(.*[AZ]){3})(?=(.*\d){3}).+ 

The reason for this is that character types may not be adjacent, for example, 3 digits may be a1b2c3 , therefore .* Allow other intermediate character types.

Note that you do not need open-end quantifiers. for example, (.*\d){3} enough to say that there are at least three digits, i.e. not ...{3,}


One final note: these leading / trailing slashes have nothing to do with regular expressions - they are an artifact of the application language. This makes the questions and answers more clear and useful for more people if they are omitted.

+7
source

Both other answers fail for a string that does not meet the requirement of โ€œat least 3 lowercase lettersโ€. Using the Bohemian answer, but supporting this case, gives the following regular expression:

^(?=(.*[^A-Za-z0-9]){3})(?=(.*[AZ]){3})(?=(.*[az]){3})(?=(.*\d){3}).+

0
source

Source: https://habr.com/ru/post/1482128/


All Articles