Sample code (runnable)
String regEx = "[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]|\n|\r|\t";
public static void main(String[] args) {
System.out.println(hasSpecialChar("", regEx));
System.out.println(hasSpecialChar(" ", regEx));
System.out.println(hasSpecialChar("faf", regEx));
System.out.println(hasSpecialChar("fa fa", regEx));
System.out.println(hasSpecialChar("fa(", regEx));
}
public static boolean hasSpecialChar(String str, String regEx) {
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.find();
}
The above code can run, but the special characters do not include the minus sign ("-"). If you want to filter the minus sign, you need to do special processing ("\\-").
String regEx1 = "[_`~!@#$%^&*()\\-+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]|\n|\r|\t";
The minus sign is special and special, as a sign.