源码如下,如果没有显示,请按F5刷新页面:
/**
* 递归过滤字符串中指定的异常字符串
* @author MENGFEIYANG
*
*/
public class CharFiltration {
private static String REGEX = "\\\\|/|:|<|>|\"";
public static String doReplace(String source) {
Matcher matcher = Pattern.compile(REGEX).matcher(source);
while (matcher.find()) {
// 匹配内容的提取.
String keytmp = matcher.group();
String defaultFormatKey = keytmp.substring(0, keytmp.length()-1);
// 第一个匹配内容替换,替换之后,再递归.
return doReplace(matcher.replaceFirst(defaultFormatKey)); // 将当前的默认关键词格式化之后,将返回的字符串递归.直到所有的默认关键词都被格式化.
}
return source;
}
}