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])?$"
精彩评论