Topic: Java Regular Expression for Filtering Special Characters
// filter special characters public static String StringFilter(String str) throws PatternSyntaxException { // only letters and numbers are allowed // String regEx = "[^a-zA-Z0-9]"; // clear all special characters String regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]"; Pattern p = Pattern.compile(regEx); Matches m = p.matches (str); return m.replaceAll("").trim(); } @Test public void testStringFilter() throws PatternSyntaxException { String str = "*adCVs*34_a _09_b5*[/435^*&chengchi()^$$&*).{}+.|.)%%*(*.China}34{45[]12.fd' *&999The following are Chinese characters ¥...{}[].,;'"'"?"; System.out.println(str); System.out.println(StringFilter(str)); }