C#用正則表達(dá)式提取文本中所有鏈接及其Href
不僅可以獲取到指定文本中所有的完整A標(biāo)簽,還可獲取Href和Name屬性
//定義正則表達(dá)式 string pattern = @"<a\s*href=(""|')(?<href>[\s\S.]*?)(""|').*?>\s*(?<name>[\s\S.]*?)</a>"; MatchCollection mc = Regex.Matches(srcString, pattern); foreach (Match m in mc) { Response.Write("{0}---{1}", m.Groups["href"].Value, m.Groups["name"].Value); }
原文鏈接:C#用正則表達(dá)式提取文本中所有鏈接及其Href