Hi,
I have string
String s = "Hello \"World\"";
when i print this on console, it prints
Hello "World"
It works fine. Now what i want to is to print
Hello \"World\"
Which means i have to replace the quotes (") with (\\) and then a
quote so that i may get a quote in output. so i used replaceAll method
System.out.println(s.replaceAll("\"", "\\\""));
But to my astonish, it prints out
Hello "World".
I wondered and then tried to do it again with
System.out.println(s.replaceAll("\"", "\\\\\""));
Now i was able to see the desired outout i.e; Hello \"World\"
But why i have to use 5 back slashes instead of 3 which were according
to the logic of "backsequences"?? I have seen the replaceAll method
and it is using the pattern matching in it.
If i try to replace the string using simple "replace" method it works
fine; i.e i don't have to use 5 backslashes, i use only 3 (according
to logic). Can someone explain why i have to use 5 slasheds in
replaceAll.
Philipp Leitner - 13 Apr 2007 08:46 GMT
Because '\' has a special meaning in a regex. Actually you have to
escape the '\' TWO TIMES when using it in a regex - first time to not
be interpreted as a special character in the Java String literal,
second time to not be interpreted as a special character in the
Regex :-)
I think I have read this somewhere in the Java API entry dealing with
Regexes some time ago. You should check there for more details.
/philipp
Btw.: this is one of the many reasons why I really /hate/ regex :-/
الصباغ - 14 Apr 2007 17:34 GMT
> Hi,
>
[quoted text clipped - 33 lines]
> to logic). Can someone explain why i have to use 5 slasheds in
> replaceAll.
now i using jbuilder7 and need newer version like what you using, can
you tell me how can i get it or if i can downlod from net.
thank you
Tris Orendorff - 14 Apr 2007 23:11 GMT
"=?utf-8?B?2KfZhNi12KjYp9i6?=" <sabd1983@yahoo.com> burped up
warm pablum in
news:1176568454.413829.298470@e65g2000hsc.googlegroups.com:
> now i using jbuilder7 and need newer version like what you using, can
> you tell me how can i get it or if i can downlod from net.
Why Soitainly! Download free versions from:
http://www.codegear.com/Downloads/TrialandFreeVersions/JBuilder/
tabid/143/Default.aspx for non-commercial use.

Signature
Tris Orendorff
[Q: What kind of modem did Jimi Hendrix use?
A: A purple Hayes.]