我想匹配html中所有的
<td> <a href="http://tw.page.bid.yahoo.com/tw/auction/b64121666?u=Y6611727893"><img src="http://tw.bid.yimg.com/ac/29/85/b64121666-ac-2463xf10x0600x0450-s.jpg"/></a></td>然后提取href的值和src的值,请高手赐教啊。
注:a标记和img标记必须要同时匹配。
小弟愚笨,只能把a和img分开匹配:
匹配所有的a标记:(<a href=\")(\\w+[a-zA-Z0-9.-?=/]*)
匹配所有的img标记:(<img src=\")(\\w+[a-zA-Z0-9.-?=/]*)

解决方案 »

  1.   

                string str = @"<td><a href=""http://tw.page.bid.yahoo.com/tw/auction/b64121666?u=Y6611727893""><img src=""http://tw.bid.yimg.com/ac/29/85/b64121666-ac-2463xf10x0600x0450-s.jpg""/></a></td>";
                Regex reg = new Regex(@"(?is)<a[^>]*?href=(['""\s]?)(?<href>[^'""\s]+)\1[^>]*?>\s*<img[^>]*?src=(['""\s]?)(?<src>[^'""\s]+)\2[^>]*?>");
                foreach (Match m in reg.Matches(str))
                {
                    Console.WriteLine(m.Groups["href"].Value);
                    Console.WriteLine(m.Groups["src"].Value);
                } Console.ReadLine();
    /*
    http://tw.page.bid.yahoo.com/tw/auction/b64121666?u=Y6611727893
    http://tw.bid.yimg.com/ac/29/85/b64121666-ac-2463xf10x0600x0450-s.jpg
    */
      

  2.   

    大哥,这个好像没有<td>的限制
      

  3.   

    没注意看到这个需求            string str = @"<a href=""1.aspx""><img src=""1.jpg""/></a><td><a href=""2.aspx""><img src=""2.jpg""/></a></td>";
                Regex reg = new Regex(@"(?is)(?<=<td[^>]*?>(?:(?!</?td).)*)<a[^>]*?href=(['""\s]?)(?<href>[^'""\s]+)\1[^>]*?>\s*<img[^>]*?src=(['""\s]?)(?<src>[^'""\s]+)\2[^>]*?>");
                foreach (Match m in reg.Matches(str))
                {
                    Console.WriteLine(m.Groups["href"].Value);
                    Console.WriteLine(m.Groups["src"].Value);
                } Console.ReadLine();
    /*
    2.aspx
    2.jpg
    */
      

  4.   

    你把
    匹配所有的a标记:(<a href=\")(\\w+[a-zA-Z0-9.-?=/]*)
    匹配所有的img标记:(<img src=\")(\\w+[a-zA-Z0-9.-?=/]*)这两个正则合并起来,然后用group的$1 $2来获取好了。