例子:<DIV id=eq_news_bg>
<!--最新更新-->
·<A href="http://www.fert.cn/news/2013/3/18/20133189395648646.shtml" title="钾长石清洁高效制钾肥工艺问世">钾长石清洁高效制钾肥工艺问世</A><BR>·<A href="http://www.fert.cn/news/2013/3/18/20133189272444456.shtml" title="全球尿素供应过剩加重">全球尿素供应过剩加重</A><BR>·<A href="http://www.fert.cn/news/2013/3/18/20133189225668437.shtml" title="大宗商品或加速下跌">大宗商品或加速下跌</A><BR>·<A href="http://www.fert.cn/news/2013/3/18/20133189162525852.shtml" title="电力体制改革两会后或提速,价改成核心抓手">电力体制改革两会后或提速,价改成核心抓手</A><BR>·<A href="http://www.fert.cn/news/2013/3/16/201331618374478785.shtml" title="2013年3月份第二周尿素市场周报(3月11日—3月16日)">2013年3月份第二周尿素市场周报(3月11日—3月16日)</A><BR>
</DIV><!--最新更新--> 是生成的注释,也要处理。
用正则表达式得到div id=eq_news_bg的里层的href地址和title的值....
<!--最新更新-->
·<A href="http://www.fert.cn/news/2013/3/18/20133189395648646.shtml" title="钾长石清洁高效制钾肥工艺问世">钾长石清洁高效制钾肥工艺问世</A><BR>·<A href="http://www.fert.cn/news/2013/3/18/20133189272444456.shtml" title="全球尿素供应过剩加重">全球尿素供应过剩加重</A><BR>·<A href="http://www.fert.cn/news/2013/3/18/20133189225668437.shtml" title="大宗商品或加速下跌">大宗商品或加速下跌</A><BR>·<A href="http://www.fert.cn/news/2013/3/18/20133189162525852.shtml" title="电力体制改革两会后或提速,价改成核心抓手">电力体制改革两会后或提速,价改成核心抓手</A><BR>·<A href="http://www.fert.cn/news/2013/3/16/201331618374478785.shtml" title="2013年3月份第二周尿素市场周报(3月11日—3月16日)">2013年3月份第二周尿素市场周报(3月11日—3月16日)</A><BR>
</DIV><!--最新更新--> 是生成的注释,也要处理。
用正则表达式得到div id=eq_news_bg的里层的href地址和title的值....
Dictionary<string,string> dic=new Dictionary<string,string>();
foreach(Match m in Regex.Matches(source,@"(?is)(?<=<DIV[^>]*?id=eq_news_bg>.*?)<A\s*href=([""']?)([^""']*?)\1[^>]*?title=([""']?)([^""]*?)\3[^>]*?>.*?</A>(?=.*?</DIV>)")
{
dic.Add(m.Groups[2].Value,m.Groups[2].Value);
}
string str = "字符串";
var list = Regex.Matches(str, @"(?is)(?<=<div[^>]*?id=[""']?eq_news_bg[""']?((?!</div>).)*?)<a(?=[^>]*?href=[""'](?<href>[^""']+)[""'])(?=[^>]*?title=[""'](?<title>[^""']+))[^>]*>").OfType<Match>().Select(t => new { href = t.Groups["href"].Value, title = t.Groups["title"].Value }).ToList();
改一下 string source = File.ReadAllText(@"C:\1.txt", Encoding.GetEncoding("GB2312"));
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach (Match m in Regex.Matches(source, @"(?is)(?<=<DIV[^>]*?id=eq_news_bg>.*?)<A\s*href=([""']?)([^""']*?)\1[^>]*?title=([""']?)([^""]*?)\3[^>]*?>.*?</A>(?=.*?</DIV>)"))
{
Console.WriteLine(m.Groups[2].Value + "\t" + m.Groups[4].Value);
}
<DIV id=eq_news_bg>
<!--最新更新-->
·<A href="http://www.fert.cn/news/2013/3/18/20133189395648646.shtml" title="钾长石清洁高效制钾肥工艺问世">钾长石清洁高效制钾肥工艺问世</A><BR>·<A href="http://www.fert.cn/news/2013/3/18/20133189272444456.shtml" title="全球尿素供应过剩加重">全球尿素供应过剩加重</A><BR>·<A href="http://www.fert.cn/news/2013/3/18/20133189225668437.shtml" title="大宗商品或加速下跌">大宗商品或加速下跌</A><BR>·<A href="http://www.fert.cn/news/2013/3/18/20133189162525852.shtml" title="电力体制改革两会后或提速,价改成核心抓手">电力体制改革两会后或提速,价改成核心抓手</A><BR>·<A href="http://www.fert.cn/news/2013/3/16/201331618374478785.shtml" title="2013年3月份第二周尿素市场周报(3月11日—3月16日)">2013年3月份第二周尿素市场周报(3月11日—3月16日)</A><BR>
</DIV>
</DIV>id=eq_news_bg 的有好多,要得到DIV class=undis id=News_2_Info 下的href和title.3楼改动的的正则可以用,能不能加个,只要id=News_2_Info 下的href和title下的。谢谢。
string pattern=@"(?is)(?<=<DIV\s*class=undis\s*id=News_2_Info[^>]*?><DIV[^>]*?id=eq_news_bg>.*?)<A\s*href=([""']?)([^""']*?)\1[^>]*?title=([""']?)([^""]*?)\3[^>]*?>.*?</A>(?=.*?</DIV>\s*</DIV)");
的name="f"没加上去,什么都没有呢
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach (Match m in Regex.Matches(source, @"(?is)(?<=<DIV\s*class=undis\s*id=News_2_Info[^>]*?>\s*<DIV[^>]*?id=eq_news_bg>.*?)<A\s*href=([""']?)([^""']*?)\1[^>]*?title=([""']?)([^""]*?)\3[^>]*?>.*?</A>(?=.*?</DIV>)"))
{
Console.WriteLine(m.Groups[2].Value + "\t" + m.Groups[4].Value);
}
你在测试数据后面加个<div><div><a href="as" title="asd">asdf</a></div></div>试试
好像还是不对,11楼改动的好像少了个DIV?前面有2个DIV,结尾只有1个DIV实际的数据只有15行。
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach (Match m in Regex.Matches(source, @"(?is)(?<=<DIV\s*class=undis\s*id=News_2_Info[^>]*?>\s*<DIV[^>]*?id=eq_news_bg>.*?)<A\s*href=([""']?)([^""']*?)\1[^>]*?title=([""']?)([^""]*?)\3[^>]*?>.*?</A>(?=(?!<DIV>).*</DIV>(?!<DIV>).*</DIV>)"))
{
Console.WriteLine(m.Groups[2].Value + "\t" + m.Groups[4].Value);
}
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach (Match m in Regex.Matches(source, @"(?is)(?<=<DIV\s*class=undis\s*id=News_2_Info[^>]*?>\s*<DIV[^>]*?id=eq_news_bg>.*?)<A\s*href=([""']?)([^""']*?)\1[^>]*?title=([""']?)([^""]*?)\3[^>]*?>.*?</A>(?=(?!(<DIV>|</DIV>)).*</DIV>(?!(<DIV>|</DIV>)).*</DIV>)"))
{
Console.WriteLine(m.Groups[2].Value + "\t" + m.Groups[4].Value);
}
string pattern = @"(?is)(?<=<DIV\s*class=undis\s*id=News_2_Info\s*name=""f""[^>]*?>\s*<DIV[^>]*?id=eq_news_bg>.*?)<A\s*href=([""']?)([^""']*?)\1[^>]*?title=([""']?)([^""]*?)\3[^>]*?>.*?</A>(?=(?!(<DIV>|</DIV>)).*</DIV>(?!(<DIV>|</DIV>)).*</DIV>)";
$("#id").attr("title");