I know this thread has been dead for two months, but it saves me a bit of typing, so please forgive me.
My question is exactly the same as the original poster's. To me, it would seem that m.find() should return false when it gets to the sixth index since there is not a sixth index in the string. But, as the answer is 01234456 in the K&B SCJP5 book and supported by my own execution of the program, I am obviously incorrect.
But why? My only explanation would be to say it's counting the null character at the end of the string, but this isn't C/C++. So I'm wildly perplexed.
Joined: Jan 15, 2006
And now a doublepost to answer my own question. My crimes increase.
Okay, when the Matcher constructor runs, it calls reset() in order to initialize its fields. Now, the one field we are looking at is the one named "to", which determines the end of the string we are looking through. This field gets set by the length of the string, so, in the case of ab34ef, the "to" field is 6.
Then the fun begins. When find() is called, it checks to see if the current index is greater than the "to" field. If not, it will search for another match. Since 6 is not greater than 6, even though we have progressed beyond the last actual index in the string, we still will search for a match. And since the pattern allows for zero characters being a match, the search succeeds regardless of input.
So, there's the answer. Now I can peacefully go back to studying. [ February 06, 2006: Message edited by: Craig Tyler ]