0

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.

Harshita Sethi
  • 2,035
  • 3
  • 24
  • 46
  • 1
    Are you always replacing the entire `var` string? Is there any reason why you can't just say `String replacement = "Replaced";`? – rgettman Oct 19 '15 at 17:49
  • @rgettman This is just an example to show what I want. I will be replacing multiple lines of a text file. In those lines, these special characters can be there. The original string will be the text file content from which I have to remove few lines. – Harshita Sethi Oct 19 '15 at 17:52

0 Answers0