public static boolean checkSpecialChar(String str) throws PatternSyntaxException {
// 清除掉所有特殊字符
String regEx = ".*[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?\\\\]+.*";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.matches();
}
public static String filterString(String str) throws PatternSyntaxException {
String regEx= "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?\\\\]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.replaceAll("_").trim();
}
public static String removeSpecialChar(String str){
String regEx= "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?\\\\ ∮g§¥∏№℡@ ® º]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.replaceAll("").trim();
}
在表达式中有特殊意义,需要添加 "\" 才能匹配该字符本身的字符汇总,特别注意:"\"字符本身在正则表示里需要用"\\\\"表示
字符 | 说明 |
^ | 匹配输入字符串的开始位置。要匹配 "^" 字符本身,请使用 "\^" |
$ | 匹配输入字符串的结尾位置。要匹配 "$" 字符本身,请使用 "\$" |
( ) | 标记一个子表达式的开始和结束位置。要匹配小括号,请使用 "\(" 和 "\)" |
[ ] | 用来自定义能够匹配 '多种字符' 的表达式。要匹配中括号,请使用 "\[" 和 "\]" |
{ } | 修饰匹配次数的符号。要匹配大括号,请使用 "\{" 和 "\}" |
. | 匹配除了换行符(\n)以外的任意一个字符。要匹配小数点本身,请使用 "\." |
? | 修饰匹配次数为 0 次或 1 次。要匹配 "?" 字符本身,请使用 "\?" |
+ | 修饰匹配次数为至少 1 次。要匹配 "+" 字符本身,请使用 "\+" |
* | 修饰匹配次数为 0 次或任意次。要匹配 "*" 字符本身,请使用 "\*" |
| | 左右两边表达式之间 "或" 关系。匹配 "|" 本身,请使用 "\|" |