主要用到的是java的 Matcher m = pattern.matcher(str); str = m.replaceAll(""); 根据对读入敏感词列表文件的每一行做匹配,来过滤敏感词 下面是具体代码: import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.Properties; import java.util.regex.Pattern; import java.util.regex.Matcher; public class KeywordFilter { private static Pattern pattern = null; public static void initPattern() { StringBuffer patternBuf = new StringBuffer(""); try { InputStream in = KeywordFilter.class.getClassLoader().getResourceAsStream("words.properties"); Properties pro = new Properties(); pro.load(in); Enumeration enu = pro.propertyNames(); while(enu.hasMoreElements()) { patternBuf.append((String)enu.nextElement()+"|"); } patternBuf.deleteCharAt(patternBuf.length()-1); pattern = Pattern.compile(new String(patternBuf.toString().getBytes("ISO-8859-1"), "UTF-8")); //System.out.println(new String(patternBuf.toString().getBytes("ISO-8859-1"), "gb2312")); //pattern = Pattern.compile(new String(patternBuf.toString().getBytes("ISO-8859-1"), "gb2312")); } catch(IOException ioEx) { ioEx.printStackTrace(); } } public static String doFilter(String str) { System.out.println("str:"+ str); try { Matcher m = pattern.matcher(str); str = m.replaceAll(""); } catch (Exception e) { e.printStackTrace(); } return str; } public static void main(String[] args) { String str = "心在跳情在烧共产党"; //String str = "�����д�һԺѧλ������д���ı�����ʾ���дʶ�,�й������,ë�