.NET Regular Expression创建STRONG密码

3 .net regex

这是我用来创建强密码的.NET正则表达式(这对我的项目密码要求不正确):

(?=^.{15,25}$)(\d{2,}[a-z]{2,}[A-Z]{2,}[!@#$%&+~?]{2,})
Run Code Online (Sandbox Code Playgroud)

密码要求:

  1. 最少15个字符(最多25个)
  2. 两个数字
  3. 两个大写字母
  4. 两个小写字母
  5. 两个特殊字符 ! @ # $ % & + ~ ?

它们不需要像我粘贴的正则表达式那样以特定的顺序彼此相邻.

上面的正则表达式需要这样的密码:12abCD!@QWertyP

它需要RE中的特定顺序......这不是我想要的!

这应该通过一个格式正确的RE与上面列出的规格:Qq1W!w2Ee#3Rr4 @ Tt5

我怎样才能消除它们彼此相邻的必要性?显然,如果该人选择密码,密码应该是随机的.

Bob*_*man 10

我认为你正在寻找的不只是正则表达式的设计目的.

考虑像这样的C#/ VB方法:

bool IsStrongPassword( String password )
{
    int upperCount = 0;
    int lowerCount = 0;
    int digitCount = 0;
    int symbolCount = 0;

    for ( int i = 0; i < password.Length; i++ )
    {
        if ( Char.IsUpper( password[ i ] ) )
            upperCount++;
        else if ( Char.IsLetter( password[ i ] ) )
            lowerCount++;
        else if ( Char.IsDigit( password[ i ] ) )
            digitCount++;
        else if ( Char.IsSymbol( password[ i ] ) )
            symbolCount++;
    }

    return password.Length >= 15 && upperCount >= 2 && lowerCount >= 2 && digitCount >= 2 && symbolCount >= 2;
}
Run Code Online (Sandbox Code Playgroud)