replaceAll
with a value that contains '*' is throwing an exception.
I tried this
String tmp = "M ******* k";
tmp = tmp.replaceAll("****","NOT");
System.out.println("TMP is :"+tmp);
and I am getting this exception
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
Why does replaceAll
with *
have this behavior and how do I overcome with this problem?
The first argument in replaceAll() takes a regular expression. The character '*' has a special meaning in regular expressions.
Replaces each substring of this string that matches the given regular expression with the given replacement.
So you have to escape it, if you want to match the literal character '*'.
Use "\\*".