字符串内容如下: <div class="list_item" id="item_239" link ="loupan/啊.html" style="padding-bottom:20px;">阿斯顿发生</div>
 <div class="list_item" id="item_99" link ="loupan/b.html" style="padding-bottom:20px;"><b>测试2</b></div>
 <div class="list_item" id="item_762" link ="loupan/c.html" style="padding-bottom:20px;"><span>测试3</span></div>
我如何能获取 div 的class="list_item" 中的值,div 的 id,link,style 属性值任意,不需管它请写出正则,谢谢最后的接果是:
阿斯顿发生
<b>测试2</b>
<span>测试3</span>

解决方案 »

  1.   


                Regex reg = new Regex("(?<=<div class=\"loupan_list_item\" id=\"[\\s\\S]*?\" link =\"[\\s\\S]*?\" style=\"padding-bottom:20px;\">)[\\s\\S]*?(?=</div>)", RegexOptions.IgnoreCase);//结束标记前空格是有区别的
                MatchCollection m = reg.Matches(str); //设定要查找的字符串 
                //Regex reg = new Regex("(?<=<li class=\"list_150 list_t_p\">)[\\s\\S]*?(?=</li>)", RegexOptions.IgnoreCase);
                //MatchCollection m = reg.Matches(str); //设定要查找的字符串 
                for (int i = 0; i < m.Count; i++)
                {
                    // myclass.newstring.CreateFile("a"+i.ToString()+".html", m[i].ToString(), System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase.ToString()+"\\Errorlog\\");                MessageBox.Show(m[i].ToString());
                }
      

  2.   

    using System;
    using System.Linq;
    using System.Xml.Linq;
    using System.IO;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string html = @"<div class=""list_item"" id=""item_239"" link =""loupan/啊.html"" style=""padding-bottom:20px;"">阿斯顿发生</div>
     <div class=""list_item"" id=""item_99"" link =""loupan/b.html"" style=""padding-bottom:20px;""><b>测试2</b></div>
     <div class=""list_item"" id=""item_762"" link =""loupan/c.html"" style=""padding-bottom:20px;""><span>测试3</span></div>";
                var doc = XDocument.Load(new StringReader("<root>" + html + "</root>"));
                var query = doc.DescendantNodes().Where(x => x.Parent != null && x.Parent.Name == "div" && x.Parent.Attribute("class").Value == "list_item");
                foreach (var item in query) Console.WriteLine(item.ToString());
            }
        }
    }阿斯顿发生
    <b>测试2</b>
    <span>测试3</span>
    Press any key to continue . . .
      

  3.   

     Regex reg = new Regex("(?<=<div class=\"loupan_list_item\" id=\"[\\s\\S]*?\" link =\"[\\s\\S]*?\" style=\"padding-bottom:20px;\">)[\\s\\S]*?(?=</div>)", RegexOptions.IgnoreCase);//结束标记前空格是有区别的这个正则不是完全正确,如果div中再嵌套一个div ,得到的值就不匹配了
    求指点
      

  4.   

     Regex reg = new Regex("(?<=<div class=\"loupan_list_item\" id=\"[\\s\\S]*?\" link =\"[\\s\\S]*?\" style=\"padding-bottom:20px;\">)[\\s\\S]*?(?=</div>)", 
    remove "?" which ed as red color.but I suggest you use this:
    (?is)<div[^>]*? class="list_item"[^>]*?>(?><div[^>]*>(?<Open>)|</div>(?<-Open>)|(?:(?!</?div\b).)*)*(?(Open)(?!))</div>
      

  5.   

     Regex reg = new Regex("(?<=<div class=\"loupan_list_item\" id=\"[\\s\\S]*?\" link =\"[\\s\\S]*?\" style=\"padding-bottom:20px;\">)[\\s\\S]*?(?=</div>)", 
    remove "?" which ed as red color.but I suggest you use this:
    (?is)<div[^>]*? class="list_item"[^>]*?>(?><div[^>]*>(?<Open>)|</div>(?<-Open>)|(?:(?!</?div\b).)*)*(?(Open)(?!))</div>
    糖糖的递归平衡组酷毙啦。