The start() method returns the starting position of the previous match because, again, we said find 0 to many occurrences.
Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
SCJP6.0,My blog Ranchers from Delhi
SCJP6.0,My blog Ranchers from Delhi
Sandra Bachan wrote:Sierra/Bates Chapter 6, Question 1
Run-time invocation:
java Regex2 "\d*" ab34ef
From what I understand, pattern is told to look for 0 or more digits. If it finds 0 or more digits, it prints the index number (invoked by m.start()) and the group that matches this pattern (invoked by m.group())
I trace the program and get the following output:
012345
However, the output of this code is
0123456
Question 1: Is this because the program counts the index past f as 0 or more occurrences of digits?
Question 2: Please clarify if I understood the following correctly
Sierra/Bates explanation of the answer says
The start() method returns the starting position of the previous match because, again, we said find 0 to many occurrences.
It seems, the find() method consumes the matcher, whereas start() returns the starting index of the consumed matcher. So if 34, which starts at index 2 is consumed, start() returns to index 2, which is printed by the System.out.print statement. Then the next matching pattern starts at index 4 because (again) 34 has been consumed
SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Returns the start index of the previous match.
Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Henry Wong wrote:http://faq.javaranch.com/java/ScjpFaq#kb-regexp
Zero-length matches can occur in several places:
a) After the last character of source data....
Preparing for SCJP