问题:想要获取如下string中的“7”还有“New York Times”//<p id="news">
      
//by <a href="/news/7?frequency=100">New York Times</a>
      
//</p>获取“7”的时候我用了如下正则式:
"(?<=<p id=\"news\">(\\s*)by <a href=\"/news/)\\d+(?=[^>]*\">)"获取“New York Times”的时候我用了:
"(?<=<p id=\"news\">by <a href=\"/news/\\d+[^>]*\">).*(?=</a>(//s*)</p>)"为什么不对呢?

解决方案 »

  1.   


            Match m = Regex.Match(str, @"<p id=""news"">\s*by <a href=""/news/(\d+)[^>]*>([^<]*)</a>\s*</p>");
            if (m.Success)
            {
                string id = m.Groups[1].Value;
                string title = m.Groups[2].Value;
            }
      

  2.   

    获取“New York Times”的时候我用了:
    "(?<=<p id=\"news\">by <a href=\"/news/\\d+[^>]*\">).*(?=</a>(//s*)</p>)"为什么不对呢?===>红色的是多的
      

  3.   

    (?i)<p([^>]*)id="news">[^<]*<a\1href="/news/(?<num>\d+)\?[^>]*>(?<word>[^<]+)</a>分别取 num 和word分组
      

  4.   


    void Main()
    {
       string html=@"//<p id=""news"">

    //by <a href=""/news/7?frequency=100"">New York Times</a>

    //</p>"; Match m =  Regex.Match(html,@"(?i)<p([^>]*)id=""news"">[^<]*<a\1href=""/news/(?<num>\d+)\?[^>]*>(?<word>[^<]+)</a>");
    if(m.Success)

    {
     Console.WriteLine(m.Groups["num"].Value);
     Console.WriteLine(m.Groups["word"].Value);
    }
     
    }/*
    7
    New York Times
    */
      

  5.   

    第二句改为:
    (?<=<p id="news">\s*by <a href="/news/\d+[^>]*">).*?(?=</a>(\s*)</p>)你那句错太多了