[英]How to modify this regular expression to be case insensitive while searching for curse words?
At the moment, this profanity filter finds darn
and golly
but not Darn
or Golly
or DARN
or GOLLY
. 此刻,此亵渎性过滤器会找到darn
和golly
但找不到Darn
或Golly
或DARN
或GOLLY
。
List<String> bannedWords = Arrays.asList("darn", "golly", "gosh");
StringBuilder re = new StringBuilder();
for (String bannedWord : bannedWords)
{
if (re.length() > 0)
re.append("|");
String quotedWord = Pattern.quote(bannedWord);
re.append(quotedWord);
}
inputString = inputString.replaceAll(re.toString(), "[No cursing please!]");
How can it be modified to be case insensitive? 如何将其修改为不区分大小写?
Start the expression with (?i)
. 以(?i)
开头表达式。
Ie, change re.toString()
to "(?i)" + re.toString()
. 即,将re.toString()
更改为"(?i)" + re.toString()
。
From the documentation of Pattern
从Pattern
的文档中
(?idmsux-idmsux)
Nothing, but turns match flagsidmsux
on - off(?idmsux-idmsux)
什么都没有,但是将匹配标志idmsux
开启-关闭
where i
is the CASE_INSENSITIVE
flag. 其中i
是CASE_INSENSITIVE
标志。
You need to set the CASE_INSENSITIVE
flag, or simply add (?i)
to the beginning of your regex. 您需要设置CASE_INSENSITIVE
标志,或简单地在正则表达式的开头添加(?i)
。
StringBuilder re = new StringBuilder("(?i)");
You'll also need to change your conditional to 您还需要将条件更改为
if (re.length() > 4)
Setting the flag via @ratchetFreak's answer is probably best, however. 但是,最好通过@ratchetFreak的答案设置标志。 It allows for your condition to stay the same (which is more intuitive) and gives you a clear idea of what's going on in the code. 它使您的条件保持不变(更直观),并让您清楚地了解代码中正在发生的事情。
For more info, see this question and in particular this answer which gives some decent explanation into using regex's in java. 有关更多信息,请参见此问题 ,尤其是此答案 ,它为在Java中使用正则表达式提供了一些不错的解释。
use a precompiled java.util.regex.Pattern 使用预编译的java.util.regex.Pattern
Pattern p = Pattern.compile(re.toString(),Pattern.CASE_INSENSITIVE);//do this only once
inputString = p.matcher(inputString).replaceAll("[No cursing please!]");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.