StanLong
package test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

     public static void main(String[] args) {
        String str = "789##$";
        String regEx="[`~!@#$%^&*()+=|{}\':;\',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]"; 
        Pattern p = Pattern.compile(regEx); 
        Matcher m1 = p.matcher(str);
        if(m1.find()){
            CharSequence cs = str;
            int j =0;
            for(int i=0; i< cs.length(); i++){
                String temp = String.valueOf(cs.charAt(i));
                Matcher m2 = p.matcher(temp);
                if(m2.find()){
                    StringBuilder sb = new StringBuilder(str);
                    str = sb.insert(j, "\\").toString();
                    j++;
                }
                j++; //转义完成后str的长度增1
            }
            System.out.println(str);
        }
    }
}

 

分类:

技术点:

相关文章:

  • 2021-09-24
  • 2021-10-03
  • 2021-10-16
  • 2021-10-16
  • 2021-10-16
  • 2021-09-24
  • 2018-07-09
  • 2021-10-16
猜你喜欢
  • 2021-11-23
  • 2021-12-18
  • 2021-09-24
  • 2021-09-24
  • 2021-11-12
  • 2020-04-08
相关资源
相似解决方案