<p style="text-align: center">
&nbsp;</p>
<p>
&nbsp;</p>
上面这段代码用什么能匹配呢?就是p标签里是空格的就匹配,p标签里有内容的不匹配。
另外,下面这段正则应该是启用了不区分大小写的单行模式了吧,那怎么不加\\s还不行呢~(?is)<p[^>]{0,}>\\s*&nbsp;\\s*</p>

解决方案 »

  1.   

                string str = @"<p style=""text-align: center"">
        &nbsp;</p>
    <p>
        &nbsp;</p>";
                Regex reg = new Regex(@"(?is)<p[^>]*?>\s*&nbsp;\s*</p>");
                foreach (Match m in reg.Matches(str))
                    Console.WriteLine(m.Value);
      

  2.   

    木有问题啊。
                string str = @"<p style=""text-align: center"">
        &nbsp;</p>
    <p>
        &nbsp;</p>";
                Regex reg = new Regex(@"(?is)<p[^>]*?>\s*&nbsp;\s*</p>");
                foreach (Match m in reg.Matches(str))
                {
                    Console.WriteLine(m.Value);
                    Console.WriteLine("===");
                } 
    /*
    <p style=""text-align: center"">
        &nbsp;</p>
    ===
    <p>
        &nbsp;</p>
    ===
    */
      

  3.   

    如果有N个"&nbsp;"呢?            string str = @"<p style=""text-align: center"">
        &nbsp;&nbsp;&nbsp;</p>";
      

  4.   

                string str = @"<p style=""text-align: center"">
        &nbsp;</p>
    <p>
        &nbsp;</p><p style=""text-align: center"">
        &nbsp;&nbsp;&nbsp;</p>";
                Regex reg = new Regex(@"(?is)<p[^>]*?>(\s*&nbsp;)+\s*</p>");
                foreach (Match m in reg.Matches(str))
                {
                    Console.WriteLine(m.Value);
                    Console.WriteLine("===");
                } 
      

  5.   

    还是不全~不会跟我用.net4.0有关系吧~
      

  6.   

    貌似用count能得出3,但用"Response.Write(reg.Matches(Finally)[i].Value + "<BR>");"得出来的就是空的。也奇怪了,用下面这段做替换就不完全匹配for (int i = 0; i < reg.Matches(Finally).Count; i++)
    {
    Response.Write("-=-"+reg.Matches(Finally)[i].Value + "<BR>");
    }用下面的就可以了~foreach (Match t in reg.Matches(Finally))
    {
    Finally = Finally.Replace(t.Value, "");
    }