I tried to check if a string Name contains letters, numbers, and underscore character with the following code without success, any idea of what I miss here?
var regex = new Regex(@"^[a-zA-Z0-9]+$^\w+$");
if (regex.IsMatch(Name) )
....
in addtion when I tried with the following code, I got a parsing error
"^[a-zA-Z0-9\_]+$" - Unrecognized escape sequence \_.
Var regex = new Regex(@"^[a-zA-Z0-9\_]+$");
The regex should be:
@"^[a-zA-Z0-9_]+$"
You don't need to escape the underscore. You can also use the Regex.Ignorecase option, which would allow you to use @"^[a-z0-9_]+$"
just as well.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句