I’m having problems with a Java Regex Pattern Syntax expression.
toCensor = toCensor.replaceAll((new
StringBuilder(("(?i)"))).append(word).toString(),
String.copyValueOf(replace));
I get the following error:
java.util.regex.PatternSyntaxException: Unmatched closing ')'
near index 4 (?i)!ick
no idea how to fix…. will give more code if needed
I don’t immediately see what’s wrong with your regex code although I suspect the problem would be apparent if we knew the values for
toCensorandword. I’ve rewritten your code as follows:So you are trying to run a regular expression across
toCentorand do a case-insensitive match (that’s the(?i)flag) looking for word. One problem is that ifwordhas any special regex characters, they will be treated as part of the pattern. I think that’s you bug. For example if you try this:You’d get the error:
This is similar but not exactly what you are seeing. You can turn off regex pattern compilation by wrapping the word in `”\Qword\E”. For example:
‘\Q’ in the pattern turns on “quoting” and
\Eis the end of it. See alsoPattern.quote(). You can also fix this by doing better sanity checking of the input to make sure that they are whole words. I suspect that)is not a proper character to be censored.