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);
}
}
}
05-11 22:42