How to handle java.util.regex.PatternSyntaxException: Dangling meta character for all the special charcters

47 Views Asked by At

I want to handle java.util.regex.PatternSyntaxException: Dangling meta character caused by any special character. For example Assume the following string

String var = "Hi * How / are \\ you + ";
String replacement = var.replaceAll(var,"Replaced");

I want to handle above code as such I need not add \\ for each replacing each special characters.

PS: There can be many more special characters other than the one mentioned. I want to create a condition to handle all the characters which would return this exception.

PPS: I have to use replaceAll. I cannot use replace as I'm working in java version 1.4. Also cannot upgrade java version.

I am stuck with version 1.4. And Pattern.quote will work on java 1.5 onwards.

0

There are 0 best solutions below