I am trying to replace the beginning of a line with backslashes with something else. For some strange reason, the replaceAll function doesn't look like a backslash.
String jarPath = "\\\\xyz\\abc\\wtf\\lame\\"; jarPath = jarPath.replaceAll("\\\\xyz\\abc", "z:");
What should I do to solve this problem.
Thank.
You need to double each backslash (again) since the Pattern class, which is used by replaceAll (), treats it as a special character:
String jarPath = "\\\\xyz\\abc\\wtf\\lame\\"; jarPath = jarPath.replaceAll("\\\\\\\\xyz\\\\abc", "z:");
Java string handles backslash as the escape-character, so replaceAll sees \\\\xyz\\abc. But replaceAll also treats the backslash as an escape character, so the regex becomes a character:\ \ x y z \ a b c
\\\\xyz\\abc
\
x
y
z
a
b
c
, \ escape- C- ( ). seperator, , MS-DOS...
, \, \\host\path \\\\host\\path , : P \\\\\\\\host\\\\path
\\host\path
\\\\host\\path
\\\\\\\\host\\\\path
,
String jarPath = "//xyz/abc/wtf/lame/"; jarPath = jarPath.replaceAll("//xyz/abc", "z:");
replaceAll() , escape-. , Java String escape-. , , , :
replaceAll , . - escape , , . , "\", ' "\" `.
replaceAll
"\"
"\\\\xyz\\abc", "\\\\\\\\xyz\\\\abc" ( \ \):
"\\\\xyz\\abc"
"\\\\\\\\xyz\\\\abc"
jarPath = jarPath.replaceAll("\\\\\\\\xyz\\\\abc", "z:");
"\" "\\" replaceAll.
replaceAll , , . String.replace :
String jarPath = "\\\\xyz\\abc\\wtf\\lame\\"; jarPath = jarPath.replace("\\\\xyz\\abc", "z:");
replace replaceAll . , .
replace(), \\\\xyz\\abc String
replace()
String
.
() replaceAll, , , 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);
:
\Media Database\_ExpiredAssets\otherfolder\anotherdeepfolder\finalfolder
, !