日期:2014-05-18 浏览次数:20993 次
string a1 = "<a href='http://www.baidu.com' class='ss'></a><a href='http://www.163.com' class='yy'></a>"; string className = "ss"; string pattern = @"(?i)<a[^>]*?(href=(['""])(?<href>[^'""]*?)\2[^>]*?)?class=(['""]?)" + className + @"\3[^>]*?(href=(['""])(?<href>[^'""]*?)\5[^>]*?>[^>]*?)?></a>"; string href = Regex.Match(a1, pattern).Groups["href"].Value;//http://www.baidu.com
------解决方案--------------------
string a1 = "<a href='http://www.baidu.com' class='ss'>baidu</a><a href='http://www.qq.com' class='yyyy'>qq</a><a href='http://www.163.com' class='yyyy'>163</a>";
string pattern = @"(?i)<a[^>]*?href=(['""])([^'""]*?)\1[^>]*>[^<]*?</a>";
var href = Regex.Matches(a1, pattern).Cast<Match>().Where(t => Regex.IsMatch(t.Value, @"class=(['""]?)yyyy\1")).Select(t => t.Groups[2].Value).ToArray();