Thanks for all those replies. But why do i get error in this case:
Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1 \ ^ at java.util.regex.Pattern.error(Unknown Source) at java.util.regex.Pattern.compile(Unknown Source) at java.util.regex.Pattern.<init>(Unknown Source) at java.util.regex.Pattern.compile(Unknown Source) at java.lang.String.replaceAll(Unknown Source) at Me.main(Me.java:11)
SCJP 5.0 77%
posted 13 years ago
Another observation related to this:
gives me an output of Hello@World
I think it has to do something with the foll , though I am not sure how.
Backslashes within string literals in Java source code are interpreted as required by the Java Language Specification as either Unicode escapes or other character escapes. It is therefore necessary to double backslashes in string literals that represent regular expressions to protect them from interpretation by the Java bytecode compiler. The string literal "\b", for example, matches a single backspace character when interpreted as a regular expression, while "\\b" matches a word boundary. The string literal "\(hello\)" is illegal and leads to a compile-time error; in order to match the string (hello) the string literal "\\(hello\\)" must be used.
"\\" is a Java string containing a single '\' character. A single '\' character is not a valid regex. To match a backslash, you need a regex with two backslashes. To create such a string in Java, you need to escape them both -- i.e., you need to write "\\\\" to match '\'!