RegEx = String "[^ A-zA-Z0-9]"; // regex the Pattern of Pattern.compile P = (regEx); // compiler String STR = "& $% ABC (@ # 2. 1"; // to match the string Matcher m = p.matcher (STR); // matching System.out.println (m.matches ()); // matches System.out.println (m.replaceAll ( "") trim . ()); // filter out the characters do not match