日期:2014-05-19  浏览次数:20834 次

正则表达式匹配网址:(c#)
string   hyperlink1= " <a   href=\ "http://www.baidu.com\ "> baidu.com </a> ";

匹配出:
http://www.baidu.com

string   hyperlink1= " <a   href=\ "http://www.baidu.com\ "> baidu.com </a> ";

匹配出:
baidu.com

string   hyperlink1= " <a   href=\ "http://www.baidu.com\ "> baidu.com </a> ";
匹配出:
http://www.baidu.com
baidu.com




------解决方案--------------------
Regex reg = new Regex(@ "(http(s)?://)?([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)? ");
------解决方案--------------------
string hyperlink1= " <a href=\ "http://www.baidu.com\ "> baidu.com </a> ";
Regex reg=new Regex(@ "href= " "([^ " "]*?) " "[^> ]*?> ([^ <]*?) < ");
Match m=reg.Match(hyperlink1);
string a=m.Groups[1].Value;
string b=m.Groups[2].Value;
------解决方案--------------------
可以匹配你所给的有无title两种情况,只需在结果中判断一下m.Groups[ "title "].Value是否为空即可

//string hyperlink1 = " <a href=\ "http://www.baidu.com\ " title= 'bai du '> baidu.com </a> ";
string hyperlink1 = " <a href=\ "http://www.baidu.com\ "> baidu.com </a> ";
Match m = Regex.Match(hyperlink1, @ " <a[^> ]*href=(? <sin1> [ ' " "]?)(? <url> [^ ' " "\s]*)(\k <sin1> )?[^> ]*?(title=(? <sin2> [ ' " "]?)(? <title> [^ ' " "]*)(\k <sin2> )?[^> ]*)?> (? <text> [^ <]*) </a> ", RegexOptions.IgnoreCase);
if(m.Success)
{
MessageBox.Show(m.Groups[ "url "].Value);
MessageBox.Show(m.Groups[ "title "].Value);
MessageBox.Show(m.Groups[ "text "].Value);
}