File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The strange String.replaceAll()

 
Albert Gan
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
Ruby Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ".
 
Albert Gan
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoa, thanks a lot, now i get it ...
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic