各位大虾,
     
     我现在要取<p align=center style='text-align:center;line-height:150%'>标签中的style性的值,或者是这样的标签
<p>,里面的什么也没有的标签。      求一个正则表达式。小弟不胜感激!谢谢了。

解决方案 »

  1.   

    不太明白,一个是p标签里面style属性,另一个是空的p标签,不是可对比的。
      

  2.   


            String regex="style\\s*=\\s*.(([^\\>:;]+:[^\\>:;]+);?){0,}";
            Pattern pattern=Pattern.compile(regex);
            Matcher m=pattern.matcher("<p align=center style='text-align:center;line-height:150%'>");
            while(m.find()){
                    System.out.println(m.group());
            }
      

  3.   

    <p [^\>] style\='([^']+)'
      

  4.   

    就是能匹配出两种类型:一种是纯的<p>
    另外一种是<p align=center style="">中的style的内容谢谢
      

  5.   


    如果这个页面中有很多的<p.....>时,我只要每个P中的style内容,应该加上P吧。有缺陷
      

  6.   

      public static void main(String[] args) {
        String str = "<p align=center style='text-align:center;line-height:150%'>123</p><p>456这个是纯p</p><p align=\"right\">789</p>";
        Pattern p = Pattern.compile("(?<=<p[^>]{0,200}style=')[^']*(?='[^>]*>)|(?<=<p>).*?(?=</p>)");
        Matcher m = p.matcher(str);
        while(m.find()){
          System.out.println(m.group());
        }
      }这样的么?好别扭啊
      

  7.   

    是什么标签前面就加什么标签就行了么。
    <p.*style\\s*=\\s*.(([^\\>:;]+:[^\\>:;]+);?){0,}";
      

  8.   


    这个代码匹配<p>这个标签时,匹配不出来。谢谢
      

  9.   


    这个<p>是否能匹配是指里面没有其他属性,而不是说该 标签下有内容
      

  10.   

    还是不太清楚,一个匹配的内容是p下的style,另一个是空的p?那空的p需要得到什么样的内容呢,如果不是空p下的内容?
      

  11.   

    说白了,就是:我要查找<p>这个类型出来,并在里面注入样式成<p....style='xxxxxxxx'>,所以,我要先找出<p>这样的标签,然后才能插入。这个又如何写呢?只匹配<p>这仆标签谢谢你的答复。
      

  12.   

    或者是可以这样理解,我想找出<p...>标签中,那些没有style="...."样式的<p>标签出生,并添加样式。
      

  13.   


    String matcher = "\\<p\\s?([^\\>]+(?<!style)\\=[^\\>\\=\\s]+\\s?)*\\>";
    String matchee = "<p align=center id='style' tt='text-align:center;line-height:150%'>ddddddd<p>ttttttttt<p style='ffff'>";
    Pattern p = Pattern.compile(matcher);
    Matcher m = p.matcher(matchee);
    while(m.find()){
    System.out.println(m.group());
    }
      

  14.   

    运行结果不太对。
    运行的结果为:
    <p align=center id='style' tt='text-align:center;line-height:150%'>
    <p>为什么这个”<p style='ffff'>“没有找出来?谢谢!
      

  15.   

    哦。你这个是反例,就是有标签没有style属性的那些情况知道了。。谢谢
      

  16.   

    GOOD , Have a look! Thanks!