[英]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? 我试图用以下代码检查字符串Name是否包含字母,数字和下划线字符,但没有成功,对我在这里错过的内容有任何想法吗?
var regex = new Regex(@"^[a-zA-Z0-9]+$^\\w+$"); 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. 您还可以使用Regex.Ignorecase选项,该选项也允许您同时使用
@"^[a-z0-9_]+$"
。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.