C# 仅用于字母数字的正则表达式不起作用

C# 仅用于字母数字的正则表达式不起作用,c#,regex,winforms,validation,C#,Regex,Winforms,Validation,这是我正在尝试的代码: string MatchNumberPattern = "^[a-zA-Z0-9]*$"; if (!Regex.IsMatch(cueTextBox9.Text, MatchNumberPattern)) { MessageBox.Show("Enter 8 Space Alphanumeric BT ID only"); cueTextBox9.Text = String.Empty; } else { do something(); }

这是我正在尝试的代码:

string MatchNumberPattern = "^[a-zA-Z0-9]*$";

if (!Regex.IsMatch(cueTextBox9.Text, MatchNumberPattern))
{
    MessageBox.Show("Enter 8 Space Alphanumeric BT ID only");
    cueTextBox9.Text = String.Empty;
}
else
{
    do something();
}

它接受
aaaaaaa
,但我想要字母和数字的组合,如
aaaa1234
要在输入中同时显示字母和数字,您需要正面外观:

(?=.*[a-zA-Z])
确保输入字符串中有字母,
(?=.*[0-9])
确保输入字符串中有数字


由于您是从单行文本框中获取输入,因此在“向前看”中使用
是安全的。作为替代,您可以使用
@^(?=[^a-zA-Z]*[a-zA-Z])(?=[^0-9]*[0-9])[a-zA-Z0-9]*$”
(基于对比原理)。

若要在输入中同时显示字母和数字,您需要正面外观:

(?=.*[a-zA-Z])
确保输入字符串中有字母,
(?=.*[0-9])
确保输入字符串中有数字

由于您是从单行文本框中获取输入,因此在“向前看”中使用
是安全的。或者,您可以使用
@^(?=[^a-zA-Z]*[a-zA-Z])(?=[^0-9]*[0-9])[a-zA-Z0-9]*$”
(基于对比原理)。

您可以使用a检查数字并在其余数字之前匹配字母

^(?i)(?=\D*\d)\d*[A-Z][A-Z\d]*$
  • ^
    字符串的开头
  • (?i)
    用于无壳匹配
  • (?=\D*\D)
    请在
    \D*
    任何数量的非数字后面加上
    \D
    数字
  • 如果成功匹配
    \d*[A-Z]
    后面跟alpha的任意数字量
  • [A-Z\d]*
    匹配任意数量的字母数字字符,直到
  • $
    字符串结尾

您可以使用a检查数字,并在其余数字之前匹配alpha

^(?i)(?=\D*\d)\d*[A-Z][A-Z\d]*$
  • ^
    字符串的开头
  • (?i)
    用于无壳匹配
  • (?=\D*\D)
    请在
    \D*
    任何数量的非数字后面加上
    \D
    数字
  • 如果成功匹配
    \d*[A-Z]
    后面跟alpha的任意数字量
  • [A-Z\d]*
    匹配任意数量的字母数字字符,直到
  • $
    字符串结尾

异步任务检查密码与密码设置(字符串密码)
{
正则表达式rxUpper=新正则表达式(@“[A-Z]”);
正则表达式rxLower=新正则表达式(@“[a-z]”);
var passwordcomplexityrulesetttings=(wait _PasswordComplexityRuleService.All()).First();
if(passwordcomplexityrulesetttings==null)
{
返回“密码有效”;
}
if(passwordcomplexityrulesetttings.MinLength>password.Length)
{
返回“密码不是最小长度”;
}
if(passwordcomplexityrulesetttings.mustContainerLettersNumber)
{
if(password.Any(a=>!char.IsDigit(a)&&&!char.islitter(a))&&&!passwordcomplexityrulesetttings.MustContainSpecialCharacters)
{
return“密码必须包含字母和数字”;
}
}
if(密码复杂性规则设置。必须包含特殊字符)
{
如果(!password.Any(a=>!char.IsDigit(a)和&!char.islitter(a)))
{
返回“密码必须包含特殊字符”;
}
}
if(PasswordComplexityRuleSettings.mustContaineUpperLower)
{
如果(!rxLower.Match(password).Success | | |!rxUpper.Match(password).Success)
{
返回“密码必须包含大小写”;
}
}
返回“密码有效”;
}
异步任务CheckPasswordAgainstPasswordSettings(字符串密码)
{
正则表达式rxUpper=新正则表达式(@“[A-Z]”);
正则表达式rxLower=新正则表达式(@“[a-z]”);
var passwordcomplexityrulesetttings=(wait _PasswordComplexityRuleService.All()).First();
if(passwordcomplexityrulesetttings==null)
{
返回“密码有效”;
}
if(passwordcomplexityrulesetttings.MinLength>password.Length)
{
返回“密码不是最小长度”;
}
if(passwordcomplexityrulesetttings.mustContainerLettersNumber)
{
if(password.Any(a=>!char.IsDigit(a)&&&!char.islitter(a))&&&!passwordcomplexityrulesetttings.MustContainSpecialCharacters)
{
return“密码必须包含字母和数字”;
}
}
if(密码复杂性规则设置。必须包含特殊字符)
{
如果(!password.Any(a=>!char.IsDigit(a)和&!char.islitter(a)))
{
返回“密码必须包含特殊字符”;
}
}
if(PasswordComplexityRuleSettings.mustContaineUpperLower)
{
如果(!rxLower.Match(password).Success | | |!rxUpper.Match(password).Success)
{
返回“密码必须包含大小写”;
}
}
返回“密码有效”;
}

当然需要
aaaaaaaa
,您指定它接受任意次数的a-z、a-z或0-9。因此也需要
aaabbbjfgfkgajffjl0142342
。你想做什么?如果只想匹配数字:
^[0-9]*$
^[0-9]+$
如果空字符串不正常。您可以使用
\d
作为简写,而不是
[0-9]
,但首先请澄清您想要什么:)我只接受字母数字,不接受字母数字,也不接受字母数字或数字。字母数字的意思是“字母数字是一个组合
  async Task<string> CheckPasswordAgainstPasswordSettings(string password)
        {
            Regex rxUpper= new Regex(@"[A-Z]");
            Regex rxLower = new Regex(@"[a-z]");



            var passwordcomplexityrulesetttings=  (await _PasswordComplexityRuleService.All()).First();
          if(passwordcomplexityrulesetttings==null)
            {
                return "password valid";
            }
          if(passwordcomplexityrulesetttings.MinLength> password.Length)
            {
                return "password is not of minimum length";

            }
            if (passwordcomplexityrulesetttings.MustContainLettersNumbers)
            {
                if(password.Any(a=>!char.IsDigit(a)&&!char.IsLetter(a)) && !passwordcomplexityrulesetttings.MustContainSpecialCharacters)
                
                {
                    return "password must contain letters and numbers";
                }


            }

            if (passwordcomplexityrulesetttings.MustContainSpecialCharacters)
            {

                if (!password.Any(a => !char.IsDigit(a) && !char.IsLetter(a)))

                {
                    return "password must contain special characters";
                }


            }
            if (passwordcomplexityrulesetttings.MustContainUpperLower)
            {

                if (!rxLower.Match(password).Success||!rxUpper.Match(password).Success)

                {
                    return "password must contain upper and lower case";
                }


            }
            
            return "password valid";

        }