Check regex for letters numbers and underscore characters
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.  
Try this regex
^[a-zA-Z0-9_-]$
You can match name with length also by this regex
^[a-zA-Z0-9_-]{m,n}$
Where
m is the start index
n is the end index
Regex Demo
Take a look at here
链接地址: http://www.djcxy.com/p/86996.html上一篇: 用逗号和句点验证正数
下一篇: 检查字母数字和下划线字符的正则表达式
