<content img_url="">
<pageinfo1 page_id="12">
<picture1  effect="" width="383" x="33" y="0" />
<picture2  effect="" width="383" x="483" y="0" />
</pageinfo1>
<pageinfo2 page_id="12">
<picture1  effect="" width="383" x="33" y="0" />
<picture2  effect="" width="383" x="483" y="0" />
</pageinfo2>
</content>如上面的xml
我想把 节点的数字去掉pageinfo1  pageinfo2 替换成 pageinfo
picture1 picture2 替换成picture
不带数字?如何写这个正侧?谢谢。

解决方案 »

  1.   

    假设上面这段xml是s
    s.replaceAll("<(\\w*)\\d{1}","<$1");
      

  2.   

    String.replaceAll("pageinfo[0-9]+","pageinfo");String.replaceAll("picture[0-9]+","picture");
      

  3.   

    killme2008 的方法不错!学习了。但是忽略了</pageinfo1>,写成下边这样就更完美了:)s.replaceAll("(<[/]?\\w*)\\d{1}","$1");
      

  4.   

    String str = "<picture2 effect=\"\" width=\"383\" x=\"483\" y=\"0\" />";
    System.out.println(str.replaceAll("(?<=[a-zA-Z]+)\\d+", ""));
      

  5.   

    s.replaceAll("(<[/]?\\w*)\\d{1}","$1");
    -----------------------------------
    解释 以下
      

  6.   


    s.replaceAll("(<[/]?\\w*)\\d{1}","$1");-----------------------------
    难点就是$1,这是分组的第一组,也就是此例中的pageinfo和picture。
    其他都没什么好解释的了,常见的正则规则
      

  7.   

    谢谢楼上这么多大大
    我也试写了一个
    public static void main(String[] args) {
    String str="pageinfo4 pageinfo5 pageinfo6 pageinfo7 picture5 picture6 picture9";
    String regEx="pageinfo[\\w]{1,10}"; 
    Pattern p=Pattern.compile(regEx);
    Matcher m=p.matcher(str);
    String s=m.replaceAll("pageinfo"); 
    boolean result=m.find();
    System.out.println(s);
    }