为什么regex101.com上的regex在C#代码中并不完全相同?例如,我想验证一个必须包含以下内容的密码:
我使用的正则表达式是:((?=.*\d)(?=.*[A-Z])(?=.*\W).{8,50000})
它可以在网站上运行。
这是示例
但是,当我将它传输到C#代码时,当我输入正确的值(例如,Testable.345
)时,它会显示一条验证消息(它不会通过)。下面是我的C#中的代码:
class Program
{
static void Main(string[] args)
{
string input = Startup();
GetMatched(input);
}
private static string Startup()
{
Console.WriteLine("Please enter string for regex match: ");
string input = Console.ReadLine();
return input;
}
private static void GetMatched(string input)
{
if (!string.IsNullOrWhiteSpace(input))
{
const string digitsRegex = "((?=.*\\d)(?=.*[A - Z])(?=.*\\W).{ 8,50000})";
Regex regex = new Regex(digitsRegex, RegexOptions.CultureInvariant);
string fieldData = input.ToString().Replace(" ", "");
if (regex.IsMatch(fieldData))
{
Console.WriteLine("Success!");
Console.ReadLine();
}
else
{
Console.WriteLine("Failed!");
Console.ReadLine();
Startup();
}
}
}
}
发布于 2020-03-23 07:16:39
/**要求:(必须至少包含每个条件中的一个):小写字母字符,大写字母字符,数字字符,非字母数字字符,最小字符串长度为8 **/。
(?=^.a-z{1}.$)(?=^.A-Z{1}.$)(?=^.0-9{1}.$)(?=^.^0-9a-zA-Z{1}.$)(?=.{8})
https://stackoverflow.com/questions/60816202
复制相似问题