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

关键字查询,在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 = new FileInputStream("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(new String(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) throws IOException{
KeyWordFilter.initPattern();
KeyWordFilter.doFilter("从前有一个小孩");
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值