例子:<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的值....

解决方案 »

  1.   

    (?is)(?<=<DIV[^>]*?id=eq_news_bg>.*?)<A\s*href=(["']?)([^"']*?)\1[^>]*?title=(["']?)(?<title>[^"]*?)\3[^>]*?>.*?</A>(?=.*?</DIV>)
    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);
    }
      

  2.   


                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();
             
      

  3.   


    改一下  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);
                }
      

  4.   

    (?is)(?<=<DIV[^>]*?id=eq_news_bg>.*?)<A\s*href=(["']?)([^"']*?)\1[^>]*?title=(["']?)(?<title>[^"]*?)\3[^>]*?>.*?</A>(?=.*?</DIV>放在VS中出现语法错误,不知道没转义还是怎么
      

  5.   

    看3楼代码string pattern=@"(?is)(?<=<DIV[^>]*?id=eq_news_bg>.*?)<A\s*href=([""']?)([^""']*?)\1[^>]*?title=([""']?)([^""]*?)\3[^>]*?>.*?</A>(?=.*?</DIV>)");
      

  6.   

    额,好像得到了很多别的DIV的,我说错了。缩小下范围:<DIV class=undis id=News_2_Info name="f">
    <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下的。谢谢。
      

  7.   

    正则改一下
    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)");
      

  8.   

    <DIV class=undis id=News_2_Info name="f">
    的name="f"没加上去,什么都没有呢
      

  9.   

    不好意思,少写一个\s* 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\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);
                }
      

  10.   

    版主这样会将div之外的<a>也查出来的。
    你在测试数据后面加个<div><div><a href="as" title="asd">asdf</a></div></div>试试
      

  11.   


    好像还是不对,11楼改动的好像少了个DIV?前面有2个DIV,结尾只有1个DIV实际的数据只有15行。
      

  12.   

    他这个应该不会你这个有12L说的嵌套div的情况吗??  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\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);
                }
      

  13.   

    这样应该可以避免你说的这种情况呢  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\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);
                }
      

  14.   

    是从别的网站用这个正则的。              string  pattern = @"(?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>)";效果还是会把别的DIV弄出来
      

  15.   

    把这个添加起
     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>)";
      

  16.   

    $("#id").attr("href");
    $("#id").attr("title");