C# 用正则表达式怎么获取A标签中的style里面的color属性? 本帖最后由 lijianguo3961 于 2012-08-03 11:08:17 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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 这个是出来了,但是,加入没有style的时候,他匹配不出来,我想让他在没有的情况下,color取空值. 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); } 好吧,还是自己研究搞定。 Regex reg = new Regex(@"(?im)<a\s*(style=""color:(?<color>[^>]*)"")?\s*[^>]*\s*href=(['""\s]?)(?<href>[^'""\s]*)\2[^>]*?>(?<text>(?:(?!</?a\b).)*)"); 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); } 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); } 简单的代码,在浏览器中却显示为空白页 用c++做.Net程序的外壳 读出文件内容 如何让程序自动运行? C#如何从网页自动下载文件? 取xml节点的问题,谢谢! DataSet.Fill()的问题 怎样做一个网页编辑器(windows form)大家给个思路 sn -k在哪里输入才能执行? 如何得到服务器端侦听的临时端口号 求正则表达式,谢谢!!! 求大神解决小小问题
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);
}}
match[i].Groups["color"].Value //#000
{
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);
}
Regex reg = new Regex(@"(?im)<a\s*(style=""color:(?<color>[^>]*)"")?\s*[^>]*\s*href=(['""\s]?)(?<href>[^'""\s]*)\2[^>]*?>(?<text>(?:(?!</?a\b).)*)");
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);
}
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);
}