开发者

Reject a string with only strings in password

开发者 https://www.devze.com 2023-03-29 20:01 出处:网络
Why is this regular expression not working? Console.WriteLine(Regex.IsMatch(password, \"(?!^[a-zA-Z]*$)\"));

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

精彩评论

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