Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

regex and \d

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any time it is written in code between double quotes "", it should have the double slash because the \ needs to be escaped by another \.

If you are writing it outside of code, like from a command line or from a text file, only one slash is necessary.

I would assume the exam will treat it exactly that way. If they are showing a code example and have regex between quotes, expect them to follow Java's rules.
 
nico dotti
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I guess what confuses me is that I thought the command line args came in as an array of string so would follow the same rules.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
nico dotti
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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[0]) 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 ]
 
nico dotti
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Huh, ok I figured it out; it was bash shell not java command interpreter - I have to use single quotes to get it to the command interpreter without my shell screwing it up LOL Here's the output:

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic