This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes String replaceAll method removes backslashes in replacement string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String replaceAll method removes backslashes in replacement string" Watch "String replaceAll method removes backslashes in replacement string" New topic
Author

String replaceAll method removes backslashes in replacement string

Raja Kannappan
Ranch Hand

Joined: May 08, 2002
Posts: 83
When I use String replaceAll() method, it removes all the backslashes in my replacement string. The matcher class in jdk explicity removes all the backslashes. Is there any way to avoid this?

when I do str.replaceAll("xyz", "c:\\temp\\xyz"); it replaces xyz with ctempxyz instead of c:\temp\xyz.

Does anyone know how to replace the string without replacing slashes? Any help is greatly appreciated. Thanks.
[ February 15, 2008: Message edited by: Raja Kannappan ]

SCJP
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
You have to use four backslashes:


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Raja Kannappan
Ranch Hand

Joined: May 08, 2002
Posts: 83
I tried that before and it worked, but I had problems converting my input replacement string (coming from user) to the desired format. Previously, I tried replacement = replacement.replaceAll("\\", "\\\\"); but it gave me error. Now, i tried replacement = replacement.replaceAll("\\\\", "\\\\\\\\"); and it works fine.

The problem is solved but I don't understand why we have to put four slashes. I understand that we put two back slashes instead of one backslash as a escape
construct so that second backslash is actually taken as a character. But, I don't understand why we have to use four slashes. Can anyone please explain?

Thanks.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
[Raja]:But, I don't understand why we have to use four slashes. Can anyone please explain?


Obviously a forward slash is a special character in a String, but a forward slash is also a special character in a regex. The forward slash has to be escaped with another forward slash in order for the regex engine to view it as a literal '\' character. Both forward slashes have to be escaped so that the String class views them as literal '\'. Therefore you end up needing four forward slashed.
Raja Kannappan
Ranch Hand

Joined: May 08, 2002
Posts: 83
Thanks Garrett. That helps!
[ February 17, 2008: Message edited by: Raja Kannappan ]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

replace foreward by backward in Garretts text.

slash=forward slash=/=uphill
backslash=\=downhill

It's a latin alphabet, so you have to read from left to right.


http://home.arcor.de/hirnstrom/bewerbung
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
[Stefan]: replace foreward by backward in Garretts text.

slash=forward slash=/=uphill
backslash=\=downhill

It's a latin alphabet, so you have to read from left to right.


Thanks for the correction. I always get those confused. Now how do I remember the difference between a stalactite and a stalagmite.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Originally posted by Garrett Rowe:

Thanks for the correction. I always get those confused. Now how do I remember the difference between a stalactite and a stalagmite.

The m in stalagmite has 3 feet at the bottom, and the t in stalagtite has a hook at the top.
John O'Hanley
Greenhorn

Joined: Oct 13, 2002
Posts: 24
There is an alternative to replaceAll that avoids regular expressions:

The String.replace(CharSequence, CharSequence) method

See as well :
javapractices.com
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String replaceAll method removes backslashes in replacement string
 
Similar Threads
String replaceAll() problem
Pattern.match() problem with $ in String
Insertion strings
capturing group, regexp
Handling '$' character