Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用密码强度验证器和正则表达式时几乎没有错误_C#_.net_Regex - Fatal编程技术网

C# 使用密码强度验证器和正则表达式时几乎没有错误

C# 使用密码强度验证器和正则表达式时几乎没有错误,c#,.net,regex,C#,.net,Regex,我遇到了一个解释如何进行密码强度验证的例子 我遇到的错误有问题。一个错误指出:无法将类型“System.Text.RegularExpressions.Match”隐式转换为“bool”,后者位于第行if(Regex.Match(password,@/\d+/”,…) 另一个错误是:运算符“&&&”不能应用于“System.Text.RegularExpressions.Match”和“System.Text.RegularExpressions.Match”类型和“System.Text.Re

我遇到了一个解释如何进行密码强度验证的例子

我遇到的错误有问题。一个错误指出:
无法将类型“System.Text.RegularExpressions.Match”隐式转换为“bool”
,后者位于第
行if(Regex.Match(password,@/\d+/”,…

另一个错误是:
运算符“&&&”不能应用于“System.Text.RegularExpressions.Match”和“System.Text.RegularExpressions.Match”类型和“System.Text.RegularExpressions.Match”
类型的操作数,这发生在执行
&
语句的行上

我不明白为什么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;
}
枚举密码分数
{
空白=0,
VeryWeak=1,
弱=2,
中等=3,
Strong=4,
VeryStrong=5
}
私有静态PasswordScore CheckStrength(字符串密码)
{
智力得分=1;
if(password.Length<1)
返回PasswordScore。为空;
如果(密码长度<4)
返回PasswordScore.VeryWeak;
如果(password.Length>=8)
分数++;
如果(password.Length>=12)
分数++;
if(Regex.Match(密码@/\d+/”,RegexOptions.ECMAScript))
分数++;
if(Regex.Match(密码@/[a-z]/”,RegexOptions.ECMAScript)&&
Regex.Match(密码@/[A-Z]/”,RegexOptions.ECMAScript))
分数++;
如果(Regex.Match(密码,@/.[,@,#,$,%,^,&,*,?,,~,-,(,)]/“,
RegexOptions.ECMAScript)
分数++;
返回(密码分数)分数;
}
Regex.Match()返回匹配对象,而不是布尔值。您可能需要检查Match.Success属性,即

var result = Regex.Match(...);
if(result.Success)
   score++;

Regex.Match
返回
Match
类型

您需要将代码更改为:

if(Regex.Match(...).Success) {
    ...
} 

或者类似的东西。

如果成功是你所关心的,那么只需使用
匹配的
成功
属性即可:

if (Regex.Match(password, @"/\d+/", RegexOptions.ECMAScript).Success)
以及:


您需要使用
IsMatch
,而不是
Match
IsMatch
返回一个
bool
,而
Match
返回一个
Match
对象,该对象提供了更多细节(捕获的组等)

PS.您知道为什么我的验证不能超过3分吗?您的正则表达式是错误的,您不应该在开头和结尾使用“/”。这是C,不是Javascript或Perl;)
if (Regex.Match(password, @"/[a-z]/", RegexOptions.ECMAScript).Success && 
    Regex.Match(password, @"/[A-Z]/", RegexOptions.ECMAScript).Success)