c# - 使用密码强度验证器和正则表达式时很少出现错误

标签 c# .net regex

我遇到了 article这解释了如何进行密码强度验证。

我对遇到的错误有疑问。一个错误状态: Cannot implicitly convert type 'System.Text.RegularExpressions.Match' to 'bool' which is on line if (Regex.Match(password, @"/\d+/",...

另一个错误是:Operator '&&' cannot be applied to operands of type 'System.Text.RegularExpressions.Match' and 'System.Text.RegularExpressions.Match' 这发生在行上AND&& 语句是。

我不明白为什么 Regex 语句没有转换为 bool 类型?第二个问题可能与第一个问题有关。

我该如何解决这个问题?

enum PasswordScore
{
    Blank = 0,
    VeryWeak = 1,
    Weak = 2,
    Medium = 3,
    Strong = 4,
    VeryStrong = 5
}

private static PasswordScore CheckStrength(string password)
{
    int score = 1;

        if (password.Length < 1)
            return PasswordScore.Blank;
        if (password.Length < 4)
            return PasswordScore.VeryWeak;

        if (password.Length >= 8)
        score++;
        if (password.Length >= 12)
            score++;
        if (Regex.Match(password, @"/\d+/", RegexOptions.ECMAScript))
            score++;
        if (Regex.Match(password, @"/[a-z]/", RegexOptions.ECMAScript) &&
            Regex.Match(password, @"/[A-Z]/", RegexOptions.ECMAScript))
            score++;
        if (Regex.Match(password, @"/.[!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]/", 
            RegexOptions.ECMAScript))
            score++;

        return (PasswordScore)score;
}

最佳答案

您需要使用IsMatch,而不是MatchIsMatch 返回一个 bool,而 Match 返回一个 Match 对象,它为您提供更多详细信息(捕获的组等)

关于c# - 使用密码强度验证器和正则表达式时很少出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8393940/

相关文章:

java - 在Java中为包含一个字母和一个数字的字符串定义正则表达式

c# - 鼠标单击 Canvas 中的对象

c# - 如何在 WinForms 中删除一个 DateTimePicker 实例?

C#,如何模拟 Azure 订阅 - 列出位置

.net - 如何结束脚本标签?

python - 找出正则表达式的每个部分匹配的内容

c# - 类构造函数方法中与 IoC 的代码契约

c# - .NET https身份验证(Pingdom API)

.net - 是否应该在生产应用程序中使用 MEF

php - 使用 PHP+CSS 检测包含大写单词的行并使用粗体行设置样式