[英]'-' not working while using Regular Expressions to match special characters, c#
Pattern is 模式是
Regex splRegExp = new System.Text.RegularExpressions.Regex(@"[\,@,+,\,?,\d,%,.,?,*,&,^,$,(,!,),#,-,_]");
All characters work except '-'. 除“-”外,所有字符均有效。 Please advise.
请指教。
Use 采用
@"[,@+\\?\d%.*&^$(!)#_-]"
No need for all those commas. 不需要所有这些逗号。
If you place a -
inside a character class , it means a literal dash only if it's at the start or end of the class. 如果将
-
放置在字符类中 ,则仅在类的开头或结尾处表示文字破折号。 Otherwise it denotes a range like AZ
. 否则,它表示一个类似于
AZ
的范围。 As Damien put it, the range ,-,
is indeed rather small (and doesn't contain the -
, of course). 正如Damien所说,范围
,-,
确实很小(当然不包含-
)。
“-”必须是您的正则表达式中的第一个字符。
Regex splRegExp = new System.Text.RegularExpressions.Regex(@"[-,\,@,+,\,?,\d,%,.,?,*,&,^,$,(,!,),#,_]");
You need to escape the -character for it to work (it's a regular expression syntax) 您需要转义-character使其起作用(这是一个正则表达式语法)
Try this: 尝试这个:
"[\,@,+,\,?,\d,%,.,?,*,&,^,$,(,!,),#,\-,_]"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.