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.
Joined: Aug 19, 2003
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
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.
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 ]