我想获取页面源代码中的链接地址
如:www.baidu.com中获取链接地址<p style=height:30px><a onClick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.baidu.com')" href=http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com>把百度设为首页</a></p><p style=height:14px><a href=http://jingjia.baidu.com>企业推广</a> | <a href=http://top.baidu.com>搜索风云榜</a> | <a href=/home.html>关于百度</a> | <a href=http://ir.baidu.com>About Baidu</a></p><p id=b>&copy;2008 Baidu <a href=http://www.baidu.com/duty>使用百度前必读</a> <a href=http://www.miibeian.gov.cn target=_blank>京ICP证030173号</a> <a href=http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001092500412><img src=http://gimg.baidu.com/img/gs.gif></a></p>得到
http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com
http://jingjia.baidu.com
http://top.baidu.com
/home.html
等地址!也就是href=后的结果我用private string getUrlCode(string StrContent)
{
string urlCode = null;
Regex re = new Regex(@"<a\s+href\s*=\s*('(?<href>[^']*)'|""(?<href>[^""]*)""|(?<href>[\S>]*))[^>]*>.*?<u>(?<link>[^<]+)</u>.*?</a>", RegexOptions.IgnoreCase | RegexOptions.Singleline);foreach (Match m in re.Matches(s))
{
urlCode = urlCode + m.Groups["href"].Value+ "\r\n";
}return urlCode;
}
没效果!

解决方案 »

  1.   

    <a.+?href=(?<href>[^>]+)>
      

  2.   


    (?<=href=).*?(?=>|\s)
      

  3.   

    try:
    MatchCollection mc = Regex.Matches(要匹配的字符串,"href=(?<href>[^>]*)",RegexOptions.IgnoreCase);
    foreach ( Match match in mc )
       Response.Write( match.Groups["href"].Value + "<br />" );
    输出:
    http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com
    http://jingjia.baidu.com
    http://top.baidu.com
    /home.html
    http://ir.baidu.com
    http://www.baidu.com/duty
    http://www.miibeian.gov.cn target=_blank
    http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001092500412