posted 18 years ago
Okay, basically, you have three things going on here...
1. The regex as written, is greedy, so it will always match the whole "apples", when it encounters it.
2. The split always go from left to right as the starting point. This means that it can't match "apples" until the start is at the "a". Furthermore, the way this regex is written, it is capable of matching nothing (zero length match).
3. The default split, that doesn't limit the number of matches, always delete any trailing zero length matches.
So...
For the first case:
The first split match is a zero length match at index zero. The second split match is "apples". And the third split match is a zero length match at the end of apples. This create a first value of zero length, a second value of a single space, a third value of zero length, and a fourth value of zero length. However, applying rule #3, the third and fourth value are deleted.
For the second case:
The first split match is apples. And the second split match is zero length right after apples. This creates a first value of zero length, a second value of zero length, and a third value of zero length. However, applying rule #3, all three values are deleted.
For the third case:
The first split match is apples. The second split match is the zero length right after apples. And the third split match is the zero length right after the space. This creates a first value of zero length, a second value of zero length, a third value of a single space, and a fourth value of zero length. However, applying rule #3, the fourth value is deleted.
[EDIT: Corrected First and Second Case -- sorry]
Henry
[ March 25, 2007: Message edited by: Henry Wong ]