GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes The strange String.replaceAll() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The strange String.replaceAll()" Watch "The strange String.replaceAll()" New topic
Author

The strange String.replaceAll()

Albert Gan
Greenhorn

Joined: Aug 19, 2003
Posts: 15
String s = "test'cumi";
System.out.println("test\\\'cumi");
System.out.println(s.replaceAll("'", "\\\\'"));
Why does it has to be 4 blackslashes instead of 3 ? =p
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

Originally posted by Albert Gan:
String s = "test'cumi";
System.out.println("test\\\'cumi");
System.out.println(s.replaceAll("'", "\\\\'"));
Why does it has to be 4 blackslashes instead of 3 ? =p

Let's number num:
\\\\'
4321
Here come the convulated logic:
- You want to escape the ', so you have \#1
- But \ is special char, so you have to escape \#1 with \#2
- oh wait, what it does just leave you with regular character \, which is not acting out as escape character
- so now you have to mark it as an escape character with \#3
- which once again need to be escape with \#4
I hope I got it right.
Albert Gan
Greenhorn

Joined: Aug 19, 2003
Posts: 15
Um ... i never intend to escape the single quote though ... and i'm quite confused with your explanation ... let's see simple examples
System.out.println("\\") == \
System.out.println("'") == System.out.println("\'") == '
so, if i want the output of \', i would use this :
System.out.println("\\'") or
System.out.println("\\\'")
And, if i want to replace the ' occurence with \', all i have to do, based on the example above is simply this :
System.out.println("'".replaceAll("'", "\\'"));
But, surprisingly to me, it prints out only ', even if you put 3 backslashes in there. It must be 4 to work correctly ...
I'm quite retarded in this problem, please help
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

The problem is that the string is going through two parsings: one by the regex parser and one by the compiler.
If you want a literal '\' in your expression, you need to escape it, right? So you want '\\' to be passed into the regex parser. But the string "\\" is changed to "\" by the compiler.
So to get the string "\\", you need to escape each character so that the compiler will translate it correctly. Thus, "\\\\". If you only had three("\\\"), you would have \"... as your string, since the first \ escapes the second \ and the third \ escapes the ".


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Albert Gan
Greenhorn

Joined: Aug 19, 2003
Posts: 15
Whoa, thanks a lot, now i get it ...
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Not to toot my own horn, but I have an entire section devoted to just this topic in my Java Regular Expressions book called the art of delimiting Strings(p.106).
toot..toot...
M
[ April 21, 2004: Message edited by: Max Habibi ]

Java Regular Expressions
 
GeeCON Prague 2014
 
subject: The strange String.replaceAll()