本文實例講述了C#提取網頁中超鏈接link和text部分的方法。分享給大家供大家參考,具體如下:
? 1 2 3 4 5 6 7 8 9string s = "..";
Regex re = new Regex(@"<a[^>]*href=(""(?<href>[^""]*)""|'(?<href>[^']*)'|(?<href>[^\s>]*))[^>]*>(?<text>.*?)</a>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match m = re.Match(s);
if(m.Success)
{
string link = m.Groups["href"].Value;
string text = Regex.Replace(m.Groups["text"].Value,"<[^>]*>","");
Console.WriteLine("link:{0}\ntext:{1}", link, text);
}