简体   繁体   English

Java:replaceAll不能与反斜杠一起使用?

[英]Java: replaceAll doesn't work well with backslash?

I'm trying to replace the beginning of a string with backslashes to something else. 我正在尝试将反斜杠替换为其他字符串。 For some weird reason the replaceAll function doesn't like backslashes. 由于某些奇怪的原因,replaceAll函数不喜欢反斜杠。

String jarPath = "\\\\xyz\\abc\\wtf\\lame\\";
jarPath = jarPath.replaceAll("\\\\xyz\\abc", "z:");

What should I do to solve this issue. 我应该怎么做才能解决这个问题。

Thank you. 谢谢。

You need to double each backslash (again) as the Pattern class that is used by replaceAll() treats it as a special character: 您需要将每个反斜杠加倍(再次),因为replaceAll()使用的Pattern类将其视为特殊字符:

String jarPath = "\\\\xyz\\abc\\wtf\\lame\\";
jarPath = jarPath.replaceAll("\\\\\\\\xyz\\\\abc", "z:");

A Java string treats backslash as an escape character so what replaceAll sees is: \\\\\\\\xyz\\\\abc . Java字符串将反斜杠视为转义字符,因此replaceAll看到的是: \\\\\\\\xyz\\\\abc But replaceAll also treats backslash as an escape character so the regular expression becomes the characters: \\ \\ x y z \\ a b c 但是replaceAll还将反斜杠视为转义字符,因此正则表达式变为以下字符: \\ \\ x y z \\ a b c

Its doesn't like it because \\ is the escape character in C like languages (even as an escape on this forum) Which makes it a poor choice for a file seperator but its a change they introduced in MS-DOS... 它不喜欢它,因为\\是类似C的语言中的转义字符(即使是此论坛上的转义符),这使其成为文件分隔符的不佳选择,但它是MS-DOS中引入的更改...

The problem you have is that you have escape the \\ twice so \\\\host\\path becomes \\\\\\\\host\\\\path in the string but for the regex has to be escaped again :P \\\\\\\\\\\\\\\\host\\\\\\\\path 您遇到的问题是您两次对\\进行了转义,因此\\\\host\\path变成了字符串中的\\\\\\\\host\\\\path ,但是对于正则表达式必须再次进行转义:P \\\\\\\\\\\\\\\\host\\\\\\\\path

If you can use a forward slash this is much simpler 如果您可以使用正斜杠,这会更简单

String jarPath = "//xyz/abc/wtf/lame/";
jarPath = jarPath.replaceAll("//xyz/abc", "z:");

replaceAll() uses regexps which uses the backslash as an escape character. replaceAll()使用正则表达式,将反斜杠用作转义字符。 Moreover, Java String syntax also uses the backslash as an escape character. 此外,Java String语法还将反斜杠用作转义字符。 This means that you need to double all your backslashes to get what you want: 这意味着您需要将所有反斜杠加倍以得到所需的内容:

String jarPath = "\\\\xyz\\abc\\wtf\\lame\\";
jarPath = jarPath.replaceAll("\\\\\\\\xyz\\\\abc", "z:");

replaceAll expects a regular expression as its input string, which is then matched and replaced in every instance. replaceAll需要一个正则表达式作为其输入字符串,然后在每个实例中将其匹配并替换。 A backslash is a special escape character in regular expressions, and in order to match it you need another backslash to escape it. 反斜杠是正则表达式中的特殊转义字符,为了与之匹配,您需要另一个反斜杠来对其进行转义。 So, to match a string with "\\" , you need a regular expression with '"\\"`. 因此,要匹配带有"\\"的字符串,您需要带有'“ \\”`的正则表达式。

To match the string "\\\\\\\\xyz\\\\abc" you need the regular expression "\\\\\\\\\\\\\\\\xyz\\\\\\\\abc" (note an extra \\ for each source \\ ): 为了匹配字符串"\\\\\\\\xyz\\\\abc"你所需要的正则表达式"\\\\\\\\\\\\\\\\xyz\\\\\\\\abc" (注意额外的\\每个源\\ ):

String jarPath = "\\\\xyz\\abc\\wtf\\lame\\";
jarPath = jarPath.replaceAll("\\\\\\\\xyz\\\\abc", "z:");
jarPath = jarPath.replaceAll("\\\\\\\\xyz\\\\abc", "z:");

对于字符串中的每个“ \\”,应将“ \\\\”放在replaceAll方法中。

The replaceAll method uses regular expressions, which means that you have to escape slashes. replaceAll方法使用正则表达式,这意味着您必须转义斜杠。 In your case it might make sense to use String.replace instead: 在您的情况下,可以改用String.replace:

String jarPath = "\\\\xyz\\abc\\wtf\\lame\\";
jarPath = jarPath.replace("\\\\xyz\\abc", "z:");

You can just use the replace method instead replaceAll in your use-case. 您可以只在用例中使用replace方法而不是replaceAll。 If i'm not mistaken it's does not use regex. 如果我没有记错的话,请不要使用正则表达式。

You can use replace() method also which will remove \\\\\\\\xyz\\\\abc from the String 您也可以使用replace()方法从String删除\\\\\\\\xyz\\\\abc

String jarPath = "\\\\xyz\\abc\\wtf\\lame\\";
jarPath = jarPath.replace("\\\\xyz\\abc", "z:");

Just got into a similar problem. 刚遇到类似的问题。

If you use backslash() in the second section of the replaceAll function, the backslashes will dissapear, to avoid that, you can use Matcher class. 如果在replaceAll函数的第二部分中使用backslash(),则反斜杠将消失,为避免这种情况,可以使用Matcher类。

String assetPath="\Media Database\otherfolder\anotherdeepfolder\finalfolder";

String assetRemovedPath=assetPath.replaceAll("\\\\Media Database(.*)", Matcher.quoteReplacement("\\Media Database\\_ExpiredAssets")+"$1");

system.out.println("ModifiedPath:"+assetRemovedPath);

Prints: 印刷品:

\Media Database\_ExpiredAssets\otherfolder\anotherdeepfolder\finalfolder

hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM