Password Regular expression for atleast 2 digit, l

2019-09-20 01:40发布

问题:

Need a Regular Expression for Passwords which must have an :

  • 8 characters minimum
  • Must include at least two of the following
    • lowercase letters
    • uppercase letters
    • numbers and
    • symbols
  • Character limit of 50

回答1:

You can use the regex as below:

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

Debuggex Demo

What I tried is combination of :

Lowercase - Uppercase   (.*?[a-z].*?[A-Z])            // Valid Entry
Lowercase - Number      (.*?[a-z].*?[0-9])            // Valid Entry
Lowercase - Symbol      (.*?[a-z].*?[!@#$%^&*()_+])   // Valid Entry

Uppercase-Number        (.*?[A-Z].*?[0-9])            // Valid Entry
Uppercase-Symbol        (.*?[A-Z].*?[!@#$%^&*()_+])   // Valid Entry

Number-Symbol           (.*?[0-9].*?[!@#$%^&*()_+])   // Valid Entry

For more from jsBin you could look into JsBin Demo

You could also look into @areschen answer, the JsBin is inspired from his fiddle.

For Reference :

Regex : Atleast 2 digits and 1 special char. Regex : I want this & that - As pointed by @Barmar



回答2:

Thank you all for your time. After multiple tries, following pattern works for me:

Note: Following expression checks "2 each" of (uppercase, lowercase, 0-9)

^(?=.{8,50}$)(?=(.*[A-Z]){2})(?=(.*[a-z]){2})(?=(.*[0-9]){2})(?=\\S+$).*$

otherwise,

^(?=(?:\\D*\\d){2})(?=(?:[^a-z]*[a-z]){2})(?=(?:[^A-Z]*[A-Z]){2})(?=\\S+$).{8,50}$

And, following checks "any 2" of (uppercase, lowercase, 0-9, defined set of specials symbols)

^(?=.{8,50}$)(?=([A-Z,a-z,0-9,[!,~,*,$]]+){2})(\\S+$)$

Description:

(?=.{8,50}$) (?= ANDs the condition to rest of conditions that follows), this condition checks for length of input from 8 to 50

(?=(.*[A-Z]){2}) (where, .* any character 0 or more times, followed by UPPERCASE letter) and {2}, means by condition should apply two times, therefore UPPERCASE letter exists 2 times

(?=(.*[a-z]){2}) (where, .* means any character 0 or more times, followed by LOWERCASE letter) and {2}, means by condition should apply two times, therefore LOWERCASE letter exists 2 times

(?=(.*[0-9]){2}) (where, .* means any character 0 or more times, followed by DIGIT ) and {2}, means by condition should apply two times, therefore DIGIT exists 2 times

(?=\\S+$) (where, \S means there non-whitspace, + means 0 or more times, so it checks if all characters are non white space characters. So if a space comes this condition will fail.

NOTE:"\" I used for java escape character .* this is last condition, which means any other character after my last check (e.g digit in my case, so it is not must, that last character should be digit.

NOTE: If there is only 1 or last condition, we don't use ?= to AND.

In java I tried this small program:

String input = "Dv1D v1bb";

String pattern = "^(?=.{8,50}$)(?=(.*[A-Z]){2})(?=(.*[a-z]){2})(?=(.*[0-9]){2})(?=\\S+$).*$";

System.out.println(Pattern.matches(pattern, input));


回答3:

This regex will work for Unicode characters as well.

^(?=.{8,50}$)(?=(.*?[\\p{Ll}].*?[\\p{Lu}])|(.*?[\\p{Ll}].*?[\\p{Nd}])|(.*?[\\p{Ll}].*?[!@#$%^&*+=?])|(.*?[\\p{Lu}].*?[\\p{Nd}])|(.*?[\\p{Lu}].*?[!@#$%^&*+=?])|(.*?[\\p{Nd}].*?[!@#$%^&*+=?])).*$