This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Matthew already said it - you have to escape it twice.
A single backslash is "\\" in Java. That's what you had. But backslash is a special character for regular expressions as well. So you need to escape it in your regular expression as well. The regular expression then becomes \\ (two backslashes), but because you need to escape these two for the Java String you get four backslashes.
And I think what was happening: the regular expression string had an escape character (single backslash) and nothing else; it blew up because it was not prepared to handle an escape character by itself as a regular expression string.
Rahul Sudip Bose wrote:I wonder what will happen if someone passes a string which uses only \ as separator to such a method. So , the person has to be aware of how he should type the strings.
Yes. The programmer must be aware that the argument to the split() method takes in a String that is a valid Regex pattern (similar to the argument you'd use for Pattern.compile())
The API documentation clearly states that the argument must be the 'delimiting regular expression' - String split