I'm attempting to censor certain words from being chatted in a game. The only issue is that the player can void my censor by adding onto the words. Here's an example.
//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);
}
The standing issue is that if someone said bilbobaggins, without a space in between, my censor can be easily avoided. How is it that I can make a sufficient censor that doesn't just check words?