<span class="auxiInfo" style="float:right;padding:5px 0px 5px 5px;">2009-9-5 10:53:49</span>
<h2><a href="/sp/article/2009/200909/20090905/article_412879.htm">Li Na reaches fourth round at US Open</a></h2>
有以上文本,要求编写正则表达式可以从这个多行并且前面有空格的文本中取出以下三个结果:2009-9-5 10:53:49
="/sp/article/2009/200909/20090905/article_412879.htm
Li Na reaches fourth round at US Open
我写了一个,如果一行文本还可以,多行就不行
string regexStr1 = "<span class=\"auxiInfo\" style=\"float:right;padding:5px 0px 5px 5px;\">(?<tdate>.*?)</span>(?s)<h2><a href=\"(?<url>.*?)\">(?<title>.*?)</a></h2>";
Regex r1 = new Regex(regexStr1, RegexOptions.Singleline);
MatchCollection mc1 = r1.Matches(strHtml1);

解决方案 »

  1.   

    try...Regex reg = new Regex(@"(?is)<span\s+class=""auxiInfo""[^>]*>(?<tdate>(?:(?!</?span\b).)*)</span> (?:(?!</?a\b).)*<a\s+href=""(?<url>[^""]*)"">(?<title>(?:(?!</?a\b).)*)</a>");
    MatchCollection mc = reg.Matches(yourStr);
    foreach (Match m in mc)
    {
        richTextBox2.Text += m.Groups["tdate"].Value + "\n";
        richTextBox2.Text += m.Groups["url"].Value + "\n";
        richTextBox2.Text += m.Groups["title"].Value + "\n";
    }
      

  2.   

    在正则表达式最前面加上 (?s) 或使用 System.Text.RegularExpressions.RegexOptions.Multiline 选项就可以是正则表达式跨行匹配。
      

  3.   

    上面说错了,应该更正为:在正则表达式最前面加上 (?s) 或使用 System.Text.RegularExpressions.RegexOptions.Singleline选项就可以是正则表达式跨行匹配。