As the other answers point out, *+
is a "possessive quantifier" It matches the previous element as many times as possible, just like a greedy quantifier, but never backtracks.
Why is this useful? Only as a performance optimization. Further, only as a performance optimization when the regex doesn't match. This is an important point to understand about regular expressions: their worst-case performance always occurs when they don't match.
Depending on the regex engine in use, and the details of the regex itself, worst-case performance can sometimes be stunningly bad. For a simple example, take this regex: a*a*a*b
, matched against this string: aaaaac
.
Confronted with this situation, a standard "NFA" type regex engine will do something like this:
- Try matching the 1st
a
5 times, the 2nd a
zero times, and the 3rd a
zero times.
- Try matching the
b
against the c
-- it fails.
- "Backtrack" and match the 1st
a
4 times, the 2nd 1 time, and the 3rd zero times.
- Try matching the
b
again -- it fails.
- Backtrack again, try the 1st
a
4 times, the 2nd zero times, and the 3rd 1 time.
- ...
- Backtrack, try the 1st
a
3 times, the 2nd 2 times, and the 3rd zero times...
(I think you can fill out the next few hundred steps by yourself.)
If the regex was a*+a*a*b
, that would never happen. It would be more like:
- Try matching the 1st
a
5 times, the 2nd zero times, and the 3rd zero times.
- Try matching the
b
-- it fails.
- Since the 1st
a
is "possessive", once it has matched 5 times, it can never try matching a smaller number of times. And since there are no a
s left in the string for the other a*
s to match, they can only match zero times. There is nothing left to try, so the match as a whole fails.
- There is no step 4. You are done. While your friends are waiting for their regexps to finish executing, you can kick back your heels and crack open your cold beverage of choice.