I'd be happy to lend a hand (as I am sure others would be), but could you please post your complete code snippet rather than pseudo code. As posted right now, it looks like the code is using some method pattern() that takes a string. There is no such method in the Java API anywhere. So I have no idea what the code is doing there. And I can only assume the find() method and start() methods in the code are the find() method from the Matcher class... but that's a big assumption.
This thread is much more confusing than it needs to be. Dash's pseudocode implies that he's matching the regex "aaaaa" against the target string "a*", which is backward. Then we learn from the the quoted text in Marilyn's reply (quoted from where, I wonder? - edit: never mind, it came from here) that the regex is really "a?", which is good, because "a*" is inconsistent with the output. Dash, this is why you should post your real code whenever possible, as Mark suggested.
Anyway, Rob's answer is still essentially correct despite the changed regex. Here's a complete demonstration: output: As you can see, each match starts where the previous one ended. Since the regex can match zero characters, it matches the empty string starting at index 5. [ July 20, 2008: Message edited by: Alan Moore ]
Joined: Jul 20, 2008
Thanks Alan for demonstrating the answer but In the String I have given five a so the first a is at position 0 and the fifth a is position at 4th position then how come 5th index is there ?
Joined: Oct 13, 2005
Please read the replies, particularly Rob Prime's which explains it simply.
Joined: Feb 25, 2006
Originally posted by Dash Abhisek: Thanks Alan for demonstrating the answer but In the String I have given five a so the first a is at position 0 and the fifth a is position at 4th position then how come 5th index is there ?
The regex "a?" in combination with the string "aaaaa" will match on the following places: