import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author fanggt
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
String str = "fuck";
String str1 = "shit";
List<String> list = new ArrayList<String>();
list.add(str);
list.add(str1);
String filterStr = isFilter(list,"fu 是ck sfshi t sf士大夫");
if(null != filterStr)
{
System.out.println("其中"+filterStr+"是禁用词!");
}
}
public static String isFilter(List<String> list,String inputWords)
{
changePattern(list);
Iterator it = list.iterator();
while(it.hasNext())
{
String patStr = (String)it.next();
Pattern pattern = Pattern.compile(patStr);
Matcher matcher = pattern.matcher(inputWords);
if(matcher.find())
{
//去掉正则表达式
return patStr.replaceAll("\\\\s\\*", "");
}
}
return null;
}
/**
* 修改list
* @param list
*/
public static void changePattern(List<String> list)
{
if(null != list && list.size()>0)
{
int index = 0;
Iterator it = list.iterator();
while(it.hasNext())
{
String str = (String) it.next();
int length = str.length();
//将字符加上正则表达式
String temp = "";
for (int i = 0; i < length; i++) {
if(i==0){
temp += str.charAt(i);
continue;
}
temp = temp + "\\s*"+str.charAt(i);
}
list.set(index, temp);
index ++;
}
}
}
}
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author fanggt
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
String str = "fuck";
String str1 = "shit";
List<String> list = new ArrayList<String>();
list.add(str);
list.add(str1);
String filterStr = isFilter(list,"fu 是ck sfshi t sf士大夫");
if(null != filterStr)
{
System.out.println("其中"+filterStr+"是禁用词!");
}
}
public static String isFilter(List<String> list,String inputWords)
{
changePattern(list);
Iterator it = list.iterator();
while(it.hasNext())
{
String patStr = (String)it.next();
Pattern pattern = Pattern.compile(patStr);
Matcher matcher = pattern.matcher(inputWords);
if(matcher.find())
{
//去掉正则表达式
return patStr.replaceAll("\\\\s\\*", "");
}
}
return null;
}
/**
* 修改list
* @param list
*/
public static void changePattern(List<String> list)
{
if(null != list && list.size()>0)
{
int index = 0;
Iterator it = list.iterator();
while(it.hasNext())
{
String str = (String) it.next();
int length = str.length();
//将字符加上正则表达式
String temp = "";
for (int i = 0; i < length; i++) {
if(i==0){
temp += str.charAt(i);
continue;
}
temp = temp + "\\s*"+str.charAt(i);
}
list.set(index, temp);
index ++;
}
}
}
}