Java String.replaceAll 不会用转义引号替换引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7200746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:03:06  来源:igfitidea点击:

Java String.replaceAll doesn't replace a quote with escaped quote

java

提问by frosty

Having a hard time replacing a quote with an escaped quote. I have a string that has the value of 'Foo "bar" foobar', and I am trying to replace the quotes around bar with escaped quotes, and it isn't working. I am going crazy.

很难用转义的引用替换引用。我有一个值为 'Foo "bar" foobar' 的字符串,我试图用转义引号替换 bar 周围的引号,但它不起作用。我快疯了。

s=s.replaceAll("\"","\\"");

I would expect s to have the value of 'foo \"bar\" foobar', but it doesn't. Any help?

我希望 s 具有 'foo \"bar\" foobar' 的值,但事实并非如此。有什么帮助吗?

回答by Jon Skeet

replaceAlluses regular expressions - in which the backslash just escapes the next character, even in the replacement.

replaceAll使用正则表达式 - 其中反斜杠只是转义下一个字符,即使在替换中也是如此。

Use replaceinstead and it's fine... or you can double the backslash in the second argument if you want to use the regular expression form:

改用replace它就好了......或者如果你想使用正则表达式形式,你可以在第二个参数中加倍反斜杠:

String after = before.replaceAll("\"", "\\\"");

This could be useful if you need to match the input using regular expressions, but I'd strongly recommend using the non-regex form unless you actually needregex behaviour.

如果您需要使用正则表达式匹配输入,这可能很有用,但我强烈建议使用非正则表达式形式,除非您确实需要正则表达式行为。

Personally I think it was a mistake for methods in Stringto use regular expressions to start with - things like foo.split(".")which will split on every characterrather than on periods, completely unexpectedly to the unwary developer.

就我个人而言,我认为String开始使用正则表达式的方法是错误的- 诸如foo.split(".")将在每个字符而不是句点上拆分的事情,对于粗心的开发人员来说完全出乎意料。

回答by Bozho

Use s = s.replace("\"", "\\\"");, which is the non-regex version.

使用s = s.replace("\"", "\\\"");,这是非正则表达式版本。

回答by Ralph

You need to escape both replace all arguments because they are regex!

您需要同时转义替换所有参数,因为它们是正则表达式!

@Test
public void testReplace() {
    assertEquals("foo\\"bar\\"",
                 "foo\"bar\"".replaceAll("\\"", "\\\\""));
}

So if you want to write "you need to escape the regex -> \"but because you are in java you need to escape the \and "for java to, so you get \\\\". (that is the search parameter).

所以如果你想写"你需要转义正则表达式 ->\"但是因为你在 java 中你需要转义\"for java to,所以你得到\\\\". (即搜索参数)。

For the replace parameter you want to have \"-> in regex: \\\"-> in Java \\\\\\\"

对于您想要的替换参数\"-> 在正则表达式中:\\\"-> 在 Java 中\\\\\\\"

回答by Taha

String str = "'Foo \"Bar\" Bar'";
System.out.println(str);        
System.out.println(str.replaceAll("\"", "\\\""));

回答by Fabian Barney

s=s.replaceAll("\"","\\\"");

But you should really go with replace(...)as the others here advice.

但你真的应该replace(...)按照其他人的建议去。

回答by Prabath Siriwardena

Use;

利用;

s=s.replaceAll("\"","\\\"");

回答by developer

please follow below program and use > s.replace("/"."//");

请遵循以下程序并使用 > s.replace("/"."//");

public class stringbackslash {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String s="\";
        s=s.replace("\", "\");
        System.out.println(s);

    }

}