日期:2014-05-20  浏览次数:20880 次

正则表达式提取url
<a   href= "http://news.qq.com/a/20070129/002200.htm "   class= "rlk_1 "   target= "_blank "> 中学生高空抛物庆祝放假 </a>
  如何用正则表达式提取http://news.qq.com/a/20070129/002200.htm出来

------解决方案--------------------
我以前做过,我觉得你最好不要用正则,表面上看其来简单,用正则不简单,因为你只是针对这些html

类似的,网页中有可能出现类似的a href,反正特例情况极多,你修改程序容易,修改正则可费事了

建议用.net文本处理功能,lastindexof(),substring(),这些字符处理方法,可以灵活的搭配,解决特例
------解决方案--------------------
string url= " <a href=\ "http://news.qq.com/a/20070129/002200.htm\ " class=\ "rlk_1\ " target=\ "_blank\ "> 中学生高空抛物庆祝放假 </a> ";
url = Regex.Replace(url,@ ".*(? <a> http://\S*.htm).* ", "${a} ", RegexOptions.IgnoreCase | RegexOptions.Compiled);

如果页面有多个地址,可根据具体内容具体匹配!