日期:2014-05-17 浏览次数:21112 次
string _str="<a style='color: #dffeaa' title=路虎1 href='http://www.baidu.com' target=_blank>路虎1</a> <a style='color: #000' title=路虎2 href='http://www.baidu.com' target=_blank>路虎2</a> <a style='color: #ccc' title=路虎3 href='http://www.baidu.com' target=_blank>路虎3</a>"; Regex reg = new Regex(@"(?is)<a[^>]*?href=(['""\s]?)(?<href>[^'""\s]*)\1[^>]*?>(?<text>(?:(?!</?a\b).)*)"); MatchCollection match = reg.Matches(_str); for (int i = 0; i < match.Count; i++) { Response.Write(match[i].Groups["text"].Value + "----" + match[i].Groups["href"].Value); }
void Main() { string _str=@"<a style='color: #dffeaa' title=路虎1 href='http://www.baidu.com' target=_blank>路虎1</a> <a style='color: #000' title=路虎2 href='http://www.baidu.com' target=_blank>路虎2</a> <a style='color: #ccc' title=路虎3 href='http://www.baidu.com' target=_blank>路虎3</a>"; Regex reg = new Regex(@"(?is)<a[^>]*?style=(['""\s]?)color:(?<color>[^'""]*?)\1[^>]*?href=(['""\s]?)(?<href>[^'""\s]*)\2[^>]*?>(?<text>(?:(?!</?a\b).)*)"); MatchCollection match = reg.Matches(_str); for (int i = 0; i < match.Count; i++) { Console.WriteLine(match[i].Groups["color"].Value); Console.WriteLine(match[i].Groups["text"].Value + "----" + match[i].Groups["href"].Value); } }
------解决方案--------------------
Regex reg = new Regex(@"(?is)<a[^>]*?style=(['""]?)[^'""]*?color:\s*?(?<color>[^'"";>]*)[^>]*?\1[^>]*?href=(['""\s]?)(?<href>[^'""\s]*)\2[^>]*?>(?<text>(?:(?!</?a\b).)*)"); match[i].Groups["color"].Value //#000
------解决方案--------------------
void Main()
{
string _str=@"<a style='color: #dffeaa' title=路虎1 href='http://www.baidu.com' target=_blank>路虎1</a>
<a style='color: #000' title=路虎2 href='http://www.baidu.com' target=_blank>路虎2</a>
<a style='color: #ccc' title=路虎3 href='http://www.baidu.com' target=_blank>路虎3</a>";
Regex reg = new Regex(@"(?is)<a\b[^>]*?(style=(?:['""\s]?)color:(?<color>[^'""]*?)\1[^>]*?)?href=(['""\s]?)(?<href>[^'""\s]*)\2[^>]*?>(?<text>(?:(?!</?a\b).)*)");
MatchCollection match = reg.Matches(_str);
}
------解决方案--------------------
[code=C#][/code]var pattern = @"<a\b(?:style\s*=\s*(?<koS>[""']?)[^""']*?color\s*:\s*(?<ColorValue>[#\w]*)[^""']*\k<koS>|\bhref\s*=\s*(?<koH>[""']?)(?<Href>[^""'\s><]*)\k<koH>|[^><])+>(?<Txt>[^<>]*)";
var testTxt = @"<a title=路虎1 href='http://www.baidu.com' target=_blank>路虎1</a>
<a style='border:1px solid #000; color: #000' title=路虎2 href='http://www.baidu.com' target=_blank>路虎2</a>
<a style='color: #ccc' title=路虎3 href='http://www.baidu.com' target=_blank>路虎3</a>";