首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# 用正则表达式如何获取A标签中的style里面的color属性

2012-11-20 
C# 用正则表达式怎么获取A标签中的style里面的color属性?C# codestring _stra stylecolor: #dffeaa

C# 用正则表达式怎么获取A标签中的style里面的color属性?

C# code
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);            }


上面的,确实可以得到,正则表达式,是我在网上面看到的,连接地址和值,我现在又想获得,style中的color咋写?这个正则表达式,咋修改?谢谢

[解决办法]
C# code
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);            }}
[解决办法]
C# code
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>";



var mcCollections = Regex.Matches(testTxt, pattern, RegexOptions.IgnoreCase);
foreach (Match mcItem in mcCollections)
{
var href = mcItem.Groups["Href"].Value;
var colorValue = mcItem.Groups["ColorValue"].Value;
var txt = mcItem.Groups["Txt"].Value;
Console.WriteLine("H:"+href+"C:".PadLeft(10)+colorValue+"T:".PadLeft(10)+txt);
}

热点排行