开发者

Reject a string with only strings in password

Why is this regular expression not working?

Console.WriteLine(Regex.IsMatch(password, "(?!^[a-zA-Z]*$)"));

As you can see the expression contains negative look ahead, so basically if the string starts and ends wi开发者_如何转开发th alphabets it should reject it. But I get always true no matter what I input. Why is this happening?


You regex matches nothing, which is not followed by a pure alphanumeric string. So every input matches that. Remember, the lookahead is not part of what is matched - it's just a condition.

Use this to match all inputs that do not start or end with alphanumeric:

"^[^a-z](.*[^a-z])?$"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜