问题是在regx模式中的"/d“显示错误。
private void txtpassword_Leave(object sender, EventArgs e) {
Regex pattern = new Regex("/^(?=.*[a-z])(?=.*[A-Z])(?=.*/d)(?=.*[#$@!%&*?])[A-Za-z/d#$@!%&*?]{10,12}$/");
if (pattern.IsMatch(txtpassword.Text)) {
MessageBox.Show("valid");
} else {
MessageBox.Show("Invalid");
txtpassword.Focus();
}
}发布于 2018-07-08 09:07:29
在regex中,反斜杠用于逃逸。所以,它应该是\d,而不是/d。
但你也可以用0-9代替。
^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[^a-zA-Z0-9\s])\S{8,}$此外,在字符串中使用反斜杠时。
然后要么双反斜杠。或者使用逐字字符串。F.e.@"foobar“
在this SO post中会有更多关于这个问题的报道。
示例C#代码:
string[] strings = { "Foo!bar0", "foobar", "Foo bar 0!", "Foo!0" };
Regex rgx = new Regex(@"^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[^a-zA-Z0-9\s])\S{8,}$");
foreach (var str in strings){
Console.WriteLine("[{0}] {1} valid.", str, rgx.IsMatch(str) ? "is" : "is not");
}返回:
[Foo!bar0] is valid.
[foobar] is not valid.
[Foo bar 0!] is not valid.
[Foo!0] is not valid.https://stackoverflow.com/questions/51229981
复制相似问题