正则表达式对敏感词的操作

引用

public static String getSensitive(String globWords){
//从数据库中得到敏感字
AppContext appContext=AppContext.getInstance();//获得配置文件
ApplicationContext ac=appContext.getAppContext();
FfxxManager ffxxManager=(FfxxManager)ac.getBean("ffxxManager");
List<Ffxx> ffxxList = ffxxManager.getFfxxList("");
        StringBuffer regex = new StringBuffer();
        regex.append("(");
     
        for(int i = 0 ; i < ffxxList.size() ; i ++)
        {
         //如果敏感词中含有"("则进行替换,否则会抛出异常
        String ffxx = ffxxList.get(i).getFfxxToPage();
        if(ffxx!=null&&!ffxx.equals("")){
        ffxx = ffxx.replaceAll("\\(","[(]");  
        }
        regex.append("(");
        regex.append(ffxx);
        regex.append(")");
        if(i != ffxxList.size()-1){
          regex.append("|");
        }
        }
        regex.append(")");
        String keywords = regex.toString();
String sensitive = "";
Pattern pattern = Pattern.compile(keywords);  
      Matcher matcher = pattern.matcher(globWords);  
      if (matcher.find()) {  
                  //得到含有的敏感词
                  sensitive = matcher.group(1);
         System.out.println(matcher.group(1));
      }  

return sensitive;
}

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