File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Locating Data via Pattern Matching Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Locating Data via Pattern Matching" Watch "Locating Data via Pattern Matching" New topic

Locating Data via Pattern Matching

Ida Achi

Joined: Oct 02, 2009
Posts: 26
With reference to K and B SCJP6 page 499. I would appreciate explanation why the answer is not

Pattern is \d\w
4 56
7 7ab

why was b ommited??

% java Regex "\d\w" "ab4 56_7ab"

Produces the output

Pattern is \d\w
4 56
7 7a

Henry Wong

Joined: Sep 28, 2004
Posts: 20357

The pattern is a digit character followed by a word charater -- aka. two characters. How can the match return three characters, when the pattern is only looking for two characters?


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
look carefully there is no quantifier so it will just look for a digit followed by any character and wiil give you that pattern only but if there was a '+' quantifier for '/w' then it would have returned everything after that unless there is a space or string has ended.

SCJP 1.6 96%
Ida Achi

Joined: Oct 02, 2009
Posts: 26
I have no idea why I had imagined seeing a quantifier. Thanks for pointing that out.
I agree. Here's the link:
subject: Locating Data via Pattern Matching
jQuery in Action, 3rd edition