正则表达式 之过滤注释

前端之家收集整理的这篇文章主要介绍了正则表达式 之过滤注释前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用java 正则表达式如何过滤掉注释呢?

如上图所示,如何去掉注释呢?

  1. @Test
  2. public void test_deleteCommen(){
  3. String input="b<!-- step的属性seq是 从1 开始的正整数 -->a";
  4. System.out.println(input.replaceAll("<!--[^<>]*-->",""));
  5. }

运行结果:

达到预期效果了.

但是

  1. @Test
  2. public void test_deleteCommen(){
  3. String input="b<!-- >step的属性seq是 从1 开始的正整数 -->a";
  4. System.out.println(input.replaceAll("<!--[^<>]*-->",""));
  5. }

运行结果:

没有达到预期的结果,为什么呢?因为注释中多了>

如何解决呢?

  1. @Test
  2. public void test_deleteComment2(){
  3. String input="b<!-- >step的属性seq是 从1 开始的正整数 -->a";
  4. String regex="<!--"+ValueWidget.otherwise22("-->")+"-->";
  5. System.out.println(input.replaceAll(regex,""));
  6. }

运行结果:

ba

达到预期了.

方法otherwise22 的实现如下:

  1. /***
  2. * 不包含
  3. * @param regex
  4. * @return
  5. */
  6. public static String otherwise22(String regex){
  7. int length=regex.length();//共有length * length种情况
  8. String[][] arr2=new String[length][];
  9. for(int i=0;i<length;i++){
  10. String[] arr3=new String[2];
  11. arr3[0]=String.valueOf(regex.charAt(i));
  12. // if(arr3[0].equals("*")){
  13. // arr3[0]="\\*";
  14. // }
  15. arr3[1]="[^"+arr3[0]+"]";
  16. // System.out.println(arr3[0]+" "+arr3[1]);
  17. arr2[i]=arr3;
  18. }
  19. // String[]result=new String[2^3];
  20. // for(int i=0;i<length;i++){
  21. // result[i]=arr2[i][0];
  22. // }
  23. // \u4E00-\u9FA5 是为了匹配汉字
  24. String normal="[\\w\u4E00-\u9FA5\\s\"']*?";
  25. List<StringBuffer> list33=assemble(arr2,true);
  26. int length22=list33.size();
  27. StringBuffer sbu=new StringBuffer("(");
  28. for(int i=1;i<length22;i++){
  29. sbu.append(normal).append(list33.get(i)).append(normal);
  30. if(i!=length22-1){
  31. sbu.append("|");
  32. }
  33. }
  34. sbu.append(")");
  35. // System.out.println(list33);
  36. return sbu.toString();
  37. }
  38.  
  39. /***
  40. *
  41. * @param a
  42. * @param aa
  43. * @param index : 初始值为0
  44. */
  45. private static List<StringBuffer> cc(String[][] aa,int index,List<StringBuffer> list,boolean isDealRegex){
  46. if(index>=aa.length){//说明已经遍历完成
  47. return list;//并不是每次循环都会执行,最后才会执行此语句.
  48. }
  49. String cc[]=aa[index];
  50. int length=cc.length;
  51. List<StringBuffer> listNew=new ArrayList<StringBuffer>();
  52. if(list==null||list.size()==0){//首次循环
  53. for(int i=0;i<length;i++){//必须保证顺序,所以不能使用 foreach
  54. if(isDealRegex && cc[i].equals("*")){
  55. cc[i]="\\*";
  56. }
  57. if(isDealRegex){
  58. listNew.add(new StringBuffer(cc[i]+"?"));
  59. }else{
  60. listNew.add(new StringBuffer(cc[i]));
  61. }
  62. }
  63. }else{
  64. for(int i=0;i<length;i++){//必须保证顺序,所以不能使用 foreach
  65. for(int j=0;j<list.size();j++){//必须保证顺序,所以不能使用 foreach
  66. StringBuffer sb=list.get(j);
  67. StringBuffer sb2=new StringBuffer(sb);
  68. if(isDealRegex && cc[i].equals("*")){
  69. cc[i]="\\*";
  70. }
  71. if(isDealRegex ){
  72. sb2.append(cc[i]+"?");
  73. }else{
  74. sb2.append(cc[i]);
  75. }
  76. listNew.add(sb2);
  77. }
  78. }
  79. }
  80. List<StringBuffer> list33=cc(aa,++index,listNew,isDealRegex);
  81. if(!ValueWidget.isNullOrEmpty(list33)){
  82. return list33;
  83. }
  84. return null;
  85. }
  86. /***
  87. * 组合
  88. * @param aa
  89. * @return
  90. */
  91. public static List<StringBuffer>assemble(String[][] aa,boolean isDealRegex){
  92. return cc(aa,null,isDealRegex);
  93. }

代码见附件io0007-find_progess-0.0.8.5-SNAPSHOT-sources.jar

猜你在找的正则表达式相关文章