强密码正则表达式

本文关键字:正则表达式 密码 | 更新日期: 2023-09-27 18:02:25

我需要一个正则表达式,它至少包含以下五个字符类中的两个:

  • 小写字符
  • 大写字符
  • 数字
  • 标点符号
  • "特别"字符(例如@#$%^&*()_+|~-=' {}[]:";'& lt;>/等等)。

这是我到目前为止所做的

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++;

但Char。IsSymbol在@ % &amp上返回false;美元。? 等

和通过regex

Regex Expression = new Regex("({(?=.*[a-z])(?=.*[A-Z]).{8,}}|{(?=.*[A-Z])(?!.*''s).{8,}})");    
bool test= Expression.IsMatch(txtBoxPass.Text);

但是我需要一个带有"OR"条件的正则表达式。

强密码正则表达式

换句话说,您想要的密码不只是包含一个"类"字符。然后可以使用

^(?![a-z]*$)(?![A-Z]*$)(?!'d*$)(?!'p{P}*$)(?![^a-zA-Z'd'p{P}]*$).{6,}$

解释:

^           # Start of string
(?![a-z]*$) # Assert that it doesn't just contain lowercase alphas
(?![A-Z]*$) # Assert that it doesn't just contain uppercase alphas
(?!'d*$)    # Assert that it doesn't just contain digits
(?!'p{P}*$) # Assert that it doesn't just contain punctuation
(?![^a-zA-Z'd'p{P}]*$) # or the inverse of the above
.{6,}       # Match at least six characters
$           # End of string