本方法獲取a標簽的href值與innerHTML:
public static string[] GetHoverTreeLinks(string objStr)
{
MatchCollection matches = Regex.Matches(objStr, @"<\s*a\s+[^>]*href\s*=\s*[""'](?<HREF>[^""']*)[""'][^>]*>(?<IHTML>[\s\S]+?)<\s*/\s*a\s*>", RegexOptions.IgnoreCase);
string[] h_links= new string[matches.Count];
int m_i = 0;
foreach (Match match in matches)
{
h_links [m_i++]= match.Groups["IHTML"].Value + ":" + match.Groups["HREF"].Value;
}
return h_links;
}
或者:
public static Dictionary<string, string> GetHoverTreeLinks(string objStr)
{
//獲取全部a標簽的innerHTML和href值
MatchCollection h_hoverTreeMatches = Regex.Matches(objStr, @"<\s*a\s+[^>]*href\s*=\s*[""'](?<HREF>[^""']*)[""'][^>]*>(?<IHTML>[\s\S]+?)<\s*/\s*a\s*>", RegexOptions.IgnoreCase);
Dictionary<string, string> h_linkInfos = new Dictionary<string, string>();
foreach (Match match in h_hoverTreeMatches)
{
h_linkInfos.Add(match.Groups["IHTML"].Value, match.Groups["HREF"].Value);
}
return h_linkInfos;
}
或者使用鍵值對泛型集合Dictionary<string, string>