日期:2014-05-18  浏览次数:21066 次

一正则表达式
string strResponse = sReader.ReadToEnd();
  /* strResponse 的内容是
   
<div class="feedback_message">
  <a onmousedown="aliclick(this,'?tracelog=reg_companylist_feedback_tp')" href="http://china.alibaba.com/message/sendrfq.htm?companyId=6665952" target="_blank" ><img src="http://img.china.alibaba.com/images/buyer/list/button_fsly_01.gif" width="69" height="21" border="0" alt="发送留言" onmouseout="swapImage(this,'http://img.china.alibaba.com/images/buyer/list/button_fsly_01.gif')" onmouseover="swapImage(this,'http://img.china.alibaba.com/images/buyer/list/button_fsly_02.gif')" /></a>
</div>

<div class="feedback_phone">
  <a href="http://betterhot.cn.alibaba.com/athena/contact/betterhot.html?contactFrom=sellofferlist_contact&keywords=热水器" target="_blank" onclick="clickAddParam(this, false,'ContactInfoClick_companylist_tp')"><img src="http://img.china.alibaba.com/images/buyer/list/button_lxfs_01.gif" width="69" height="21" border="0" alt="点此查看卖家电话、传真、地址等具体联系方式。" onmouseout="swapImage(this,'http://img.china.alibaba.com/images/buyer/list/button_lxfs_01.gif')" onmouseover="swapImage(this,'http://img.china.alibaba.com/images/buyer/list/button_lxfs_02.gif')" /></a>
  </div>
  */

  string Pattern =@"<div class=""feedback_phone""><a href=""[\s\S]*?""";
  //我这个正则表达式有问题,要取得是
  <div class="feedback_phone">
  <a href="http://betterhot.cn.alibaba.com/athena/contact/betterhot.html?contactFrom=sellofferlist_contact&keywords=热水器" 的href的值也就是http://betterhot.cn.alibaba.com/athena/contact/betterhot.html?contactFrom=sellofferlist_contact&keywords=热水器

  MatchCollection Matchs = Regex.Matches(strResponse, Pattern, RegexOptions.IgnoreCase);
  foreach(Match NextMatch in Matchs)
  {
  string contectUrl= NextMatch.ToString();
   
  }
请问该正则表达式怎么写??

------解决方案--------------------
如果你只要href,那你可以这样,用这个正则就可以了
C# code

(?<=<a href=")[^"]*(?=")

MatchCollection Matchs = Regex.Matches(strResponse, "(?<=<a href=\")[^\"]*(?=\")", RegexOptions.IgnoreCase); 
foreach(Match NextMatch in Matchs) 
{ 
    string contectUrl=  NextMatch.Value;//你要的
}

------解决方案--------------------
<a href="(?<urlText>[^"]*)"
----------------------------
这个正则我试过了,不行呢。。。。

用两次正则可以捕捉到
先用
<a href=[\w\W]*?>
然后再用
(?<=href=)[\w\W]*?(?=target)