I know that the k&b book goes over this in detail, but what is confusing to me is that how I should interpret \d on the exam. On my system I seem to be able to get away with \d if it's used as a command line arg, but if I use a String I have to use the \\d version. I've seen mock questions (unfortunately I can't remember exactly where or which ones) that ask you a question assuming that the \d (or \s \w, etc.) WILL work. I'm finding this confusing and wonder what the exam creators expect...it seems to be completely system/jvm dependent. Should I just assume that it's being parsed into a pattern correctly unless the question's wording alludes to it potentially being dangerous?
The distinction is that String argument provided at the command prompt is not a String literal in the source code. As explained in the API under java.util.regex.Pattern...
Backslashes within string literals in Java source code are interpreted 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.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Oh, ok I get it the command args (although put into a String array) are not considered String literals themselves so they're not treated the same as a String literal. Got it. So I did a test just printing out the first arg (args) and don't quite understand the rules for how java strips the escape sequence/backslashes - Here's my console input and output for a program that just prints out the first arg:
What's really confusing me is that when I use '\d' it seems to completely strip the backslash - but if it's working as a digit metacharacter for a search pattern it must NOT be stripping it. Is this happening in java or my shell (bash)? I don't know how to debug this to see what's really going on. Where can I read more about exactly how backslashes are stripped under the hood. Wow, this is basic and I better figure this out fast before my test!
[ December 06, 2007: Message edited by: nico dotti ] [ December 06, 2007: Message edited by: nico dotti ]