Java: replaceAll doesn't work well with backslash?
Asked Answered
A

9

2

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.

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

What should I do to solve this issue.

Thank you.

Adley answered 16/4, 2012 at 9:45 Comment(0)
B
4

You need to double each backslash (again) as the Pattern class that is used by replaceAll() treats it as a special character:

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. But replaceAll also treats backslash as an escape character so the regular expression becomes the characters: \ \ x y z \ a b c

Beecher answered 16/4, 2012 at 9:49 Comment(0)
P
3

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...

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

If you can use a forward slash this is much simpler

String jarPath = "//xyz/abc/wtf/lame/";
jarPath = jarPath.replaceAll("//xyz/abc", "z:");
Peta answered 16/4, 2012 at 9:49 Comment(1)
It's an example string. The actual string is a Windows share path, so I can't replace it.Adley
G
2

replaceAll() uses regexps which uses the backslash as an escape character. Moreover, Java String syntax also uses the backslash as an escape character. 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:");
Grannia answered 16/4, 2012 at 9:49 Comment(0)
B
2

replaceAll expects a regular expression as its input string, which is then matched and replaced in every instance. 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 \):

String jarPath = "\\\\xyz\\abc\\wtf\\lame\\";
jarPath = jarPath.replaceAll("\\\\\\\\xyz\\\\abc", "z:");
Bellied answered 16/4, 2012 at 9:50 Comment(0)
C
1
jarPath = jarPath.replaceAll("\\\\\\\\xyz\\\\abc", "z:");

For each '\' in your string you should put '\\' in the replaceAll method.

Contingence answered 16/4, 2012 at 9:49 Comment(1)
For each '\' in your answer you should put '\\' in the mrkup.Outfit
C
1

The replaceAll method uses regular expressions, which means that you have to escape slashes. In your case it might make sense to use String.replace instead:

String jarPath = "\\\\xyz\\abc\\wtf\\lame\\";
jarPath = jarPath.replace("\\\\xyz\\abc", "z:");
Cliquish answered 16/4, 2012 at 9:53 Comment(0)
A
0

You can just use the replace method instead replaceAll in your use-case. If i'm not mistaken it's does not use regex.

Appetizer answered 16/4, 2012 at 9:51 Comment(0)
S
0

You can use replace() method also which will remove \\\\xyz\\abc from the String

String jarPath = "\\\\xyz\\abc\\wtf\\lame\\";
jarPath = jarPath.replace("\\\\xyz\\abc", "z:");
Swagerty answered 16/4, 2012 at 9:51 Comment(0)
H
0

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.

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!

Haematinic answered 13/3, 2018 at 18:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.