已知html标签,例如“...<div>...<div>...</div>...</div>...”如何正确读取一个标签的内的文本,听说可以用正则不知道咋写的

解决方案 »

  1.   

    http://topic.csdn.net/u/20090814/11/2b26498c-feb0-4c9e-b03c-4e286c45b4cf.html
      

  2.   

    string s = "<body><div>你好</div><div id=\"div1\">我好</div></body>";
    MatchCollection matches=Regex.Matches(s, @"(?is)<div[^>]*>(.*?)</div>");
    foreach (Match match in matches)
    Response.Write(match.Groups[1].Value + "<br/>");
      

  3.   

    当<div>标签内部再套一个<div>标签的时候就截取了错误的结尾标签
      

  4.   

    http://www.cnblogs.com/qiantuwuliang/archive/2011/06/11/2078329.html
      

  5.   

                string str = "div前面<div id=\"d\">外面div<div>里面div</div>外面div</div>div后面";
                Regex reg = new Regex(@"(?is)<div[^>]*?id=""d""[^>]*?>(?:(?!</?div).)*(((?<Open><div[^>]*?>)(?:(?!</?div).)*)*((?<-Open></div>)(?:(?!</?div).)*)*)*(?(Open)(?!))</div>");
                Console.WriteLine(reg.Match(str).Value);
      

  6.   

    Regex regex = new Regex( ">([^<]*)(>?<a[^<]*</a)*", RegexOptions.IgnoreCase );
      

  7.   

    这个不错,可惜js或perl下不能用