在 Java 中用 '\\' 替换单个 '\'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14183248/
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-31 15:23:54  来源:igfitidea点击:

Replacing single '\' with '\\' in Java

javaregexstringpatternsyntaxexception

提问by Husain Sanwerwala

How do I replace a single '\'with '\\'? When I run replaceAll()then I get this error message.

如何更换一个'\'带有'\\'?当我运行时,replaceAll()我收到此错误消息。

Exception in thread "main" java.util.regex.PatternSyntaxException:
                           Unexpected internal error near index 1 \
                                                                  ^
    at java.util.regex.Pattern.error(Pattern.java:1713)
    at java.util.regex.Pattern.compile(Pattern.java:1466)
    at java.util.regex.Pattern.<init>(Pattern.java:1133)
    at java.util.regex.Pattern.compile(Pattern.java:823)
    at java.lang.String.replaceAll(String.java:2190)
    at NewClass.main(NewClass.java:13)
Java Result: 1

My code:

我的代码:

public class NewClass {
    public static void main(String[] args) {
        String str = "C:\Documents and Settings\HUSAIN\My Documents\My Palettes";
        str = str.replaceAll("\", "\\");
        System.out.println(str);
    }
}

回答by Mike

String.replaceAll(String,String)is regex.
String.replace(String,String)will do what you want.

String.replaceAll(String,String)是正则表达式。
String.replace(String,String)会做你想做的。

The following...

下列...

String str = "C:\Documents and Settings\HUSAIN\My Documents\My Palettes";
System.out.println(str);
str = str.replace("\", "\\");
System.out.println(str);

Produces...

产生...

C:\Documents and Settings\HUSAIN\My Documents\My Palettes
C:\\Documents and Settings\\HUSAIN\\My Documents\\My Palettes

C:\Documents and Settings\HUSAIN\My Documents\My Palettes
C:\\Documents and Settings\\HUSAIN\\My Documents\\My Palettes

回答by Adam Sznajder

\is also a special character in regexp. This is why you should do something like this:

\也是正则表达式中的一个特殊字符。这就是为什么你应该做这样的事情:

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

回答by Javier Diaz

You have to first scape the \for the string and then scape it for the regex, it would be \\\\for each slash.

您必须首先\为字符串转义,然后为正则表达式转义它,这将\\\\用于每个斜杠。

回答by JB Nizet

In a String literal, \must be escaped with another \. And in a reges, a \must also be escaped by another \\. So, you must escape every \four times: \\\\.

在字符串文字中,\必须用另一个\. 在 a reges 中, a\也必须被 another 转义\\。因此,您必须每\四次转义:\\\\.

Another way is to use Pattern.quote("\\")(for the regex) and Matcher.quoteReplacement("\\\\")for the replacement string.

另一种方法是使用Pattern.quote("\\")(用于正则表达式)和Matcher.quoteReplacement("\\\\")用于替换字符串。

回答by Tom Leese

You could use Pattern.quoteto make it easier for you to escape the value, such as:

您可以使用Pattern.quote使您更容易转义该值,例如:

str = str.replaceAll(Pattern.quote("\"), Matcher.quoteReplacement("\\"));

or, you can just use String.replace:

或者,您可以只使用String.replace

str = str.replace("\", "\\");

See: Pattern.quote, String.replaceand Matcher.quoteReplacement

请参阅:Pattern.quoteString.replaceMatcher.quoteReplacement

回答by Manish Giri

filePath = filePath.replaceAll(Matcher.quoteReplacement("\"), Matcher.quoteReplacement("\\"));

filePath = filePath.replaceAll(Matcher.quoteReplacement("\"), Matcher.quoteReplacement("\\"));

This one worked perfectly. filePath = C:\abc\

这个工作得很好。文件路径 = C:\abc\