Can someone explain me what is the difference between [0-9]+
and [0-9]++
?

- 378,754
- 76
- 643
- 1,055

- 1,195
- 3
- 17
- 26
-
2This isn't quite a duplicate, but should answer the question: http://stackoverflow.com/questions/4489551/what-is-double-plus-in-regular-expressions – Spudley May 31 '11 at 11:06
-
I've never heard of `++` in regex, and it seems plenty of others haven't either, but a bit of digging does show that it is valid. – Spudley May 31 '11 at 11:10
-
2Don't write "solved" in titles. Accept an answer. – Lightness Races in Orbit May 31 '11 at 11:15
2 Answers
The PCRE engine, which PHP uses for regular expressions, supports "possessive quantifiers":
Quantifiers followed by
+
are "possessive". They eat as many characters as possible and don't return to match the rest of the pattern. Thus.*abc
matches"aabc"
but.*+abc
doesn't because.*+
eats the whole string. Possessive quantifiers can be used to speed up processing.
And:
If the PCRE_UNGREEDY option is set (an option which is not available in Perl) then the quantifiers are not greedy by default, but individual ones can be made greedy by following them with a question mark. In other words, it inverts the default behaviour.
The difference is thus:
/[0-9]+/ - one or more digits; greediness defined by the PCRE_UNGREEDY option
/[0-9]+?/ - one or more digits, but as few as possible (non-greedy)
/[0-9]++/ - one or more digits, but as many as possible (greedy, default)
This snippet visualises the difference when in greedy-by-default mode. Note that the first snippet is functionally the same as the last, because the additional +
is (in a sense) already applied by default.
This snippet visualises the difference when applying PCRE_UNGREEDY (ungreedy-by-default mode). See how the default is reversed.

- 73,866
- 12
- 100
- 156

- 378,754
- 76
- 643
- 1,055
-
-
-
2
-
@user557108: You are most welcome. And credit to Spudley for wisening us all up. :) – Lightness Races in Orbit May 31 '11 at 11:19
-
Sorry @Tomalak, but this answer is wrong, too. In particular, that stuff about the PCRE_UNGREEDY option is a total red herring; there is no connection whatsoever between PCRE_UNGREEDY and possessive quantifiers. Possessive quantifiers are fundamentally different from both normal, greedy quantifiers and non-greedy quantifiers. That description you quoted from the PHP docs is worse than useless; [here's a better one](http://www.regular-expressions.info/possessive.html). – Alan Moore Jun 01 '11 at 00:00
-
@AlanMoore: Don't the two examples demonstrate the behaviour aptly? I shall come back to this tomorrow and have a closer look. In the meantime, feel free to post a contrasting answer. :) – Lightness Races in Orbit Jun 01 '11 at 00:02
-
Actually, @Tim has already posted a correct answer. His example shows how, if the possessive quantifier would need to backtrack in order to achieve an overall match, it lets the match fail instead. Your examples merely prove that possessive quantifiers are always greedy, which is true but irrelevant. – Alan Moore Jun 01 '11 at 01:07
++
(and ?+
, *+
and {n,m}+
) are called possessive quantifiers.
Both [0-9]+
and [0-9]++
match one or more ASCII digits, but the second one will not allow the regex engine to backtrack into the match if that should become necessary for the overall regex to succeed.
Example:
[0-9]+0
matches the string 00
, whereas [0-9]++0
doesn't.
In the first case, [0-9]+
first matches 00
, but then backtracks one character to allow the following 0
to match. In the second case, the ++
prevents this, therefore the entire match fails.
Not all regex flavors support this syntax; some others implement atomic groups instead (or even both).

- 378,754
- 76
- 643
- 1,055

- 328,213
- 58
- 503
- 561