java关键字匹配(java去除敏感字原…

来源:互联网 发布:sql重复数据删除 编辑:程序博客网 时间:2024/06/05 18:13
关键字查询,在web开发中,可能应用到的场景:
建立***.properties 文件,文件中写入关键字。例如itil中常用的:服务器,路由器......
传入的参数往往是用户在客户端输入的内容。当作doFilter(String str)方法中的参数str。
一下的代码能够匹配出客户输入的信息中所含的关键字 m.group(),用作查询,统计等相关功能。


public class KeyWordFilter {
private static Pattern pattern = null;
       public static void initPattern() {
StringBuffer patternBuf = new StringBuffer("");
try {
FileInputStream in = newFileInputStream("words/words.properties");
Properties pro = new Properties();
pro.load(in);
Enumeration enu = pro.propertyNames();
patternBuf.append("(");
while (enu.hasMoreElements()) {
patternBuf.append((String) enu.nextElement() + "|");
}
patternBuf.deleteCharAt(patternBuf.length() - 1);
patternBuf.append(")");
          System.out.println(patternBuf);
// unix换成UTF-8
// pattern = Pattern.compile(new
// String(patternBuf.toString().getBytes("ISO-8859-1"),"UTF-8"));
// win下换成gb2312
//pattern = Pattern.compile(newString(patternBuf.toString()
// .getBytes("ISO-8859-1"), "gb2312"));
           pattern=Pattern.compile(patternBuf.toString());
} catch (IOException ioEx) {
ioEx.printStackTrace();
}
}

public static String doFilter(String str) {
Matcher m = pattern.matcher(str);
while (m.find())//查找符合pattern的字符串  
       {
               System.out.println("The result is here :" +  
                m.group()); 
              }  
return str;
}
public static void main(String[] args) throwsIOException{
KeyWordFilter.initPattern();
KeyWordFilter.doFilter("从前有一个小孩");
 
}
0 0
原创粉丝点击