I am trying to delete/ replace whole words from a string.
I would like to do so case-insensitive and it should also work for special caracters, such as .
,\
or /
.
Do do so, I use the following code:
String result = Pattern.compile(stringToReplace, Pattern.LITERAL | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE).matcher(inputString)
.replaceAll("");
Like this, it works for special characters and it is case insensitive.
I know that I can enable whole word matching by using "\b".
I could do the following:
String result = Pattern.compile("\\b"+stringToReplace+"\\b", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE).matcher(inputString)
.replaceAll("");
This way it would match only whole words, but there would be problems for special characters. It interferes with Pattern.LITERAL. I need to disable this, which is not desired.
How can I combine Pattern.LITERAL with whole word matching?