我试图审查某些词语在游戏中被聊天。唯一的问题是玩家可以通过添加单词来取消我的审查。这是一个例子。
//Check for rude words before sending to server
List<String> tokens = new ArrayList<String>();
tokens.add("bilbo");
tokens.add("baggins");
tokens.add("in");
tokens.add("the");
tokens.add("shire");
String patternString = "\\b(" + StringUtils.join(tokens, "|") + ")\\b";
Pattern pattern = Pattern.compile(patternString);
Matcher findRudeWords = pattern.matcher(result.toLowerCase());
while (findRudeWords.find()) {
//Replace the bad word with astericks
String asterisk = StringUtils.leftPad("", findRudeWords.group(1).length(), '*');
result = result.replaceAll("(?i)" + findRudeWords.group(1), asterisk);
}
常见问题是如果有人说bilbobaggins,中间没有空格,我的检查员可以很容易地避免。我怎么能做一个不仅仅检查单词的足够的审查?
答案 0 :(得分:1)
取出两个字的边界。两个\ b's。我不想打扰StringUtils所需的额外库,所以我稍微修改了你的代码,但这是我测试过的:
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.*;
class Ideone {
public static void main(String[] args) throws java.lang.Exception {
// Check for rude words before sending to server
String result = "heres bilbobaggins haha";
String patternString = "(bilbo|baggins|in|the|shire)";
Pattern pattern = Pattern.compile(patternString);
Matcher findRudeWords = pattern.matcher(result.toLowerCase());
while (findRudeWords.find()) {
// Replace the bad word with asterisks
result = result.replaceAll("(?i)" + findRudeWords.group(1), "*");
}
System.out.println("result=" + result);
}
}
输出:
result=heres ** haha
你可以在这里玩:http://ideone.com/72SU7X