Wednesday, June 3, 2009

JAVA: PatternSyntaxException: Dangling meta character '*' near index

Pattern Syntax Exception is thrown when you have "*" in any String and you want to replace "*" with something else.
This exception is likely when you use method java.lang.String.replaceAll().

There are two solutions to this problem.
Solution 1: escape all metacharacters and use replaceAll().

Solution 2:
I prefer this solution over solution 1. This method reduces most of the work as you don't need to escape any metacharacters.
Just use org.apache.commons.lang.StringUtils