首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

正则表达式提取url解决办法

2012-02-06 
正则表达式提取urlahref http://news.qq.com/a/20070129/002200.htm class rlk_1 target _blank

正则表达式提取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);

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

热点排行