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.
The trick is in what "0 or more" means and what it does.
string "AB34EF" pattern "\d*"
Okay, '*' says search for "0" or more numeric digits. As it steps through the string, checking the digits it will return with a match for every check that returns 0 or more digits.
When you check 'A' there are 0 digits so matcher returns 0 for the index and the group() is null.
When you check 'B' there are 0 digits so matcher returns 1 for the index and the group() is null.
When you check '3' there is a digit but we need to keep searching so no return is made.
When you check '4' there is a digit and we need to continue searching...
When you check the 'E' that ends the sequence of digits so two things happen For the digits we found, matcher returns 2 for the index and the group() is "34". Because the match used indexes 2 and 3, the next search will start at index 4 and it has 0 digits so matcher returns 4 and group() as null.
When you check 'F' 0 digits are found so matcher returns with 5 for the index and group() is null.
One more check is made to find the end of the string and it returns 6 as the index and I imagine null as the string.
As an intermediate step to illustrate how the answer is formed let's stack all of these together as follows...
Since null is actuall "no string at all" let's remove all of the nulls:
I may be a little fuzzy on what happens at the end of the string (the index 6) but I believe this reasonably well explains what happens on this question. I hope that it will be of some help.
(edited to correct typos) [ October 06, 2008: Message edited by: Bob Ruth ]
SCJP - 86% - June 11, 2009