Maybe a simpler example is comparing this to this .
Regular expressions used:
(a*)*
and
(a+)*
And the test line is aaaaaa .
What happens is that after capturing the main group (in the example I gave, series a ), he tries to match more, but cannot. But wait! It may also mean nothing, because * means 0 or more times!
Therefore, after matching all a 's, it will match and catch "nothing", and since only the last captured part is saved, you get "" as a result of the capture group.
In (a+)* after matching and capturing aaaaaa it can no longer match or catch ( + does not allow it to match nothing, not * ), and therefore aaaaaa is the last match.
source share