【发布时间】:2014-03-26 10:06:01
【问题描述】:
我正在尝试制作一个简单的程序来根据正则表达式验证字符串,但我遇到了一个异常。这是我的代码:
String exp = "^\\d\\d*$";
Regex r = new Regex(Regex.Unescape(exp));
if (r.IsMatch(""))
{
Response.Write("strings matches");
}
else
{
Response.Write("strings does not matches");
}
但是这段代码会产生异常:
exception.Message = "parsing \"^\\d\\d*$\" - Unrecognized escape sequence \\d."
exception.GetType() ={Name = "ArgumentException" FullName = "System.ArgumentException"} System.Type {System.RuntimeType}
谁能告诉我为什么会这样?
【问题讨论】:
-
使用@使字符串不再使用转义字符\像这样
String exp = @"^\\d\\d*$";
标签: c# regex exception escaping