强密码的正则表达式
我需要一个包含以下五个字符类中至少两个的正则表达式:
- 小写字符
- 大写字符
- 数字
- 标点
- “特殊”字符(例如
@#$%^&*()_+|~-=\
{} []:“;’ /`等)
这是我到目前为止所做的
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在@%和$上返回false。 ? 等等..
并通过正则表达式
Regex Expression = new Regex("({(?=.*[az])(?=.*[AZ]).{8,}}|{(?=.*[AZ])(?!.*\\s).{8,}})"); bool test= Expression.IsMatch(txtBoxPass.Text);
但我需要一个带有“OR”条件的正则表达式。
换句话说,您需要一个不仅包含一个“类”字符的密码。 然后你可以使用
^(?![az]*$)(?![AZ]*$)(?!\d*$)(?!\p{P}*$)(?![^a-zA-Z\d\p{P}]*$).{6,}$
说明:
^ # Start of string (?![az]*$) # Assert that it doesn't just contain lowercase alphas (?![AZ]*$) # 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