java敏感字过滤

读取敏感字配置文件,或者数据库,使用正则表达式工具进行过滤
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestFilter {
private static Pattern pattern = null;

public static void initPattern() {
StringBuffer patternBuf = new StringBuffer();
try {
InputStream in = TestFilter.class.getClassLoader()
.getResourceAsStream("config.properties");
Properties properties = new Properties();
properties.load(in);

Enumeration<?> enu = properties.propertyNames();
while (enu.hasMoreElements()) {
patternBuf.append((String) enu.nextElement() + "|"); // 读取所有properties里的词,以
// | 分隔
System.out.println(new String(patternBuf.toString().getBytes(
"UTF-8"), "UTF-8"));
}

patternBuf.deleteCharAt(patternBuf.length() - 1);

// 默认下,properties文件读取编码: ISO8859-1
pattern = Pattern.compile(new String(patternBuf.toString()
.getBytes("UTF-8"), "UTF-8"));

} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 是否包含敏感字
*
* @param str
* @return true:是 false:否
*/
public static boolean isContainsKeywords(String str) {
String temp = str;
try {
Matcher m = pattern.matcher(str);
str = m.replaceAll("*");
} catch (Exception e) {
e.printStackTrace();
}
return temp.equals(str) ? false : true;
}

/**
* 获取用*代替掉敏感字的字符串
*
* @param str
* @return
*/
private static Map<Boolean, String> doFilter(String str) {
Map<Boolean, String> test = new HashMap<Boolean, String>();
String origi = str;
Matcher m = pattern.matcher(str);
if (!m.find()) {
test.put(false, str);
} else {
m.replaceFirst("2");
int start = m.end() - m.group().length();
int end = m.end();
String startStsr = str.substring(0, start);
String endStr = str.substring(end, str.length());
str = startStsr + endStr;
int i = origi.length() - str.length();
String mm = "";
for (int k = 0; k < i; k++) {
mm += "*";
}
str = startStsr + mm + endStr;
test.put(true, str);
}
return test;
}

public static String filterKeywords(String str) {
Map<Boolean, String> test1 = new HashMap<Boolean, String>();
test1 = doFilter(str);
while (!test1.containsKey(false)) {
str = test1.get(true);
test1 = doFilter(str);
}
return str;
}

public static void main(String[] args) {
initPattern();
String string = "你他妈是个傻逼死贱人";
System.out.println("endString:" + filterKeywords(string));
}

你可能感兴趣的:(java,正则表达式)