我想使用正则表达式来匹配html文件中的h1-h6的标题,取得里面的数据,请问这个正则表达式该怎么写呢,谢谢!

解决方案 »

  1.   


                string str = "<h1>h1h1h1h1h1h1h1</h1>aaaaaaaaa<h2>h2h2h2h2h2h2</h2>";
                Regex reg = new Regex(@"(?is)<h([1-6])[^>]*?>((?!</?\1).)*</h\1>");
                foreach (Match m in reg.Matches(str))
                {
                    Response.Write(m.Value + "<br/>");
                }
    /*
    <h1>h1h1h1h1h1h1h1</h1>
    <h2>h2h2h2h2h2h2</h2>
    */
      

  2.   

    (?i)<h[1-6][^>]*>([^<]+)</h[1-6]>
      

  3.   

    已经可以匹配到了,谢谢楼上的几位!
    请问如何得到里面的数据呢?
    比如:
    <h1 style="margin-top: 0px; margin-bottom: 0px">12345</h1>
    请问如何得到12345呢?
      

  4.   

    foreach(Match m in Regex.Matches(yourHtml,@"(?is)<(h\d)\b[^>]*>(?<text>((?!</\1).)+)</\1>"))
    {
        m.Groups["text"].Value;//就是你要的12345
        m.Value;//就是完整一条
    }