关键字过滤 java_java 关键字过滤

以下实现对关键字的过滤:

两个个文件words.properties和KeyFilter.java;

1、words.properties配置关键字的配置文件;内容如下:

name1

name2

name3

2、KeyFilter.java:

import java.io.IOException;

import java.io.InputStream;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Enumeration;

import java.util.Properties;

import java.util.regex.Pattern;

import java.util.regex.Matcher;

public class KeyFilter

{

private static Pattern pattern = null;

// 从words.properties初始化正则表达式字符串

private static void initPattern() {

StringBuffer patternBuffer = new StringBuffer();

try {

InputStream in = KeyFilter.class.getClassLoader().getResourceAsStream("words.properties");

Properties property = new Properties();

property.load(in);

Enumeration enu = property.propertyNames();

patternBuffer.append("(");

while (enu.hasMoreElements()) {

String scontent = (String) enu.nextElement();

patternBuffer.append(scontent + "|");

System.out.println(scontent);

}

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

patternBuffer.append(")");

// 装换编码

pattern = Pattern.compile(patternBuffer.toString());

} catch (IOException ioEx) {

ioEx.printStackTrace();

}

}

private static String doFilter(String str) {

initPattern();

Matcher m = pattern.matcher(str);

// 选择替换方式,这里以* 号代替         str = m.replaceAll("*");         return str;     }          public static void main(String[] args) {         String str = "我日,艹,fuck,你妹的 干啥呢";         System.out.println(doFilter(str));     } }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值