使用
[\W+]或
"[^a-zA-Z0-9]"作为正则表达式来匹配任何特殊字符,还可以使用String.replaceAll(regex,String)将spl字符替换为空字符串。请记住,作为String.replaceAll的第一个arg是一个正则表达式,您必须使用反斜杠对其进行转义以将em视为文字字符。
String c= "hjdg$h&jk8^i0ssh6"; Pattern pt = Pattern.compile("[^a-zA-Z0-9]"); Matcher match= pt.matcher(c); while(match.find()) { String s= match.group(); c=c.replaceAll("\"+s, ""); } System.out.println(c);


