10

I have input string like "\\{\\{\\{testing}}}" and I want to remove all "\". Required o/p: "{{{testing}}}".

I am using following code to accomplish this.

protected String removeEscapeChars(String regex, String remainingValue) {
    Matcher matcher = Pattern.compile(regex, Pattern.CASE_INSENSITIVE).matcher(remainingValue);
    while (matcher.find()) {
        String before = remainingValue.substring(0, matcher.start());
        String after = remainingValue.substring(matcher.start() + 1);
        remainingValue = (before + after);
    }
    return remainingValue;
}

I am passing regex as "\\\\{.*?\\\\}".

Code is working fine only for 1st occurrence of "\{" but not for all its occurrences. Seeing the following outputs for different inputs.

  1. i/p : "\\{testing}" - o/p: "{testing}"
  2. i/p : "\\{\\{testing}}" - o/p: "{\\{testing}}"
  3. i/p : "\\{\\{\\{testing}}}" - o/p: "{\\{\\{testing}}}"

I want "\" should be removed from the passed i/p string, and all "\\{" should be replaced with "{".

I feel the problem is with regex value i.e., "\\\\{.*?\\\\}".

Can anyone let me know what should be the regex value to the get required o/p.

Baz
  • 36,440
  • 11
  • 68
  • 94
MIM
  • 499
  • 3
  • 11
  • 30
  • Why not just use something **replaceAll("\\", "")** you can just add other characters to it as well... – ramsinb Sep 14 '12 at 11:01
  • Are you passing as an input `"\\{testing}"` or `"\{testing}"`? The last one won't compile. – J.A.I.L. Sep 14 '12 at 11:06
  • My outputs for your examples are: `i/p: "\\{testing}" - o/p: "\testing}"` `i/p: "\\{\\{testing}}" - o/p: "\\{testing}}"` `i/p: "\\{\\{\\{testing}}}" - o/p: "\\{\{testing}}}"` – J.A.I.L. Sep 14 '12 at 11:07
  • Maybe I'm not understanding your question however seems to me replaceAll will do it. Here is an example that will remove the characters '\', '|' and '/' **replaceAll("[\\\\]*[|]*[/]*", "")** – ramsinb Sep 14 '12 at 11:22
  • My last comment with outputs different to those you said, were with the regex shown before @Baz's edition: `"\\{.*?\\}"`. With the current regex (`"\\\\{.*?\\\\}"`) you get a `PatternSyntaxException`. – J.A.I.L. Sep 14 '12 at 11:29
  • @J.A.I.L. Sorry, if I changed the content of the question somehow. Just wanted to improve the structure. – Baz Sep 14 '12 at 11:35
  • @Baz No, you didn't change the content. You only changed the way it's shown. It happens that adding the ` makes the escape char \ not to work. You made the code look correct. Thanks. – J.A.I.L. Sep 14 '12 at 11:44

4 Answers4

11

Any reasons why you are not simply using String#replace?

String noSlashes = input.replace("\\", "");

Or, if you need to remove backslashes only when they are before opening curly braces:

String noSlashes = input.replace("\\{", "{");
assylias
  • 321,522
  • 82
  • 660
  • 783
  • The method is generic method and I am using to escape different characters. I will build the regex value based on characters to be removed. Here my code is working only for 1st occurance not for others. – MIM Sep 14 '12 at 11:03
  • @user1073430 What I'm saying is that you seem to be rewriting a method that already exists. You can also use `replaceAll` if you want to use regex. – assylias Sep 14 '12 at 11:06
2

It should be as simple as the following:

String result = remainingValue.replace("\\", "");
Dan D.
  • 32,246
  • 5
  • 63
  • 79
1

As it's been answered before, if you only want to remove the slashes \ before the {, the best way is just to use

String noSlashes = input.replace("\\{", "{");

But in your question you asked Can anyone let me know what should be the regex value. If you are using regular expressions because you want to remove the \ not just before any {, but only in those { that are properly closed later with }, then the answer is: NO. You can't match nested {} with regex.

Community
  • 1
  • 1
J.A.I.L.
  • 10,644
  • 4
  • 37
  • 50
-2

Change the RegEx: "\\&([^;]{6})"

private String removeEscapeChars(String remainingValue) {
        Matcher matcher = Pattern.compile("\\&([^;]{6})", Pattern.CASE_INSENSITIVE).matcher(remainingValue);
        while (matcher.find()) {
            String before = remainingValue.substring(0, matcher.start());
            String after = remainingValue.substring(matcher.start() + 1);
            remainingValue = (before + after);
        }
        return remainingValue;
    }

it should work..

Dev
  • 2,326
  • 24
  • 45