如何从JAVA中的字符串中删除转义字符

MIM*_*MIM 9 java regex

我有输入字符串"\\{\\{\\{testing}}}",我想删除所有"\".要求o/p : "{{{testing}}}".

我正在使用以下代码来完成此任务.

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;
}
Run Code Online (Sandbox Code Playgroud)

我正在通过正则表达式"\\\\{.*?\\\\}".

代码仅在第一次出现"\ {"时正常工作,但不适用于所有出现的情况.查看以下输出以了解不同的输入.

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

我想要"\"从传递的i/p字符串中删除,所有"\\{"应该替换为"{".

我觉得问题在于正则表达式值,即"\\\\{.*?\\\\}".

任何人都可以让我知道什么应该是获得所需的o/p的正则表达式值.

ass*_*ias 12

你不是简单地使用的任何理由String#replace

String noSlashes = input.replace("\\", "");
Run Code Online (Sandbox Code Playgroud)

或者,如果您只需要在打开花括号之前删除反斜杠:

String noSlashes = input.replace("\\{", "{");
Run Code Online (Sandbox Code Playgroud)