日期:2014-05-17  浏览次数:20738 次

【请教】正则表达式在C#中的转义问题 !!!!!!!!
本帖最后由 tohen 于 2013-09-27 17:09:45 编辑
因为有多个正则表达式,所以为了方便管理,将它们存入数据库中。
这些在“正则表达式测试器”上能正常运行,
但是带入C#中却捕捉不到相应的内容。
请问要怎么解决呢?

举个例子:
某个正则表达式为:
(?<=<li><a\s*href=\\")(http:[^">]+)">([^<]+)</a><span\s*[^>]*time[^>]*>(\d{4}-\d{2}-\d{2})

C#中的代码如下,其中变量strRegList为读取数据库中的正则表达式

    foreach (Match match in Regex.Matches(sb.ToString(), strRegList, RegexOptions.Singleline))
    {
        strHttp = match.Groups[1].Value.ToString();
        strTitle = match.Groups[2].Value.ToString();
        strDate = match.Groups[3].Value.ToString();
    }


谢谢大家了!
正则表达式 c#

------解决方案--------------------
引用:
Quote: 引用:

sb.ToString()是什么呢??贴出来


<li><a href=\"http://www.gd.gov.cn/gdgk/gdyw/201309/t20130926_185903.htm\">第95期\"广东学习论坛\"报告会举行 胡春华出席报告会</a><span class=\"time\">2013-09-26</span></li>\n           \t\n\t\t\t\t<li><a href=\"http://www.gd.gov.cn/gdgk/gdyw/201309/t20130927_185931.htm\">朱小丹率省政府代表团在法国访问 推动双方经贸合作</a><span class=\"time\">2013-09-27</span></li>\n           \t\n\t\t\t\t<li><a href=\"http://www.gd.gov.cn/gdgk/gdyw/201309/t20130926_185906.htm\">广州航海学院正式揭牌 陈云贤出席推动学院发展座谈会</a><span class=\"time\">2013-09-26</span></li>\n

估计还是转义字符造成的吧
你上面给的html字符串,像引号这些都是转义了的,其实没有\这个符号的,只是用来转义引号的,所以正则表达式改成
(?<=<li><a\s*href=\")(http:[^">]+)\">([^<]+)</a><span\s*[^>]*time[^>]*>(\d{4}-\d{2}-\d{2})