In java, what's the difference between "\\d+"
and "\\d++"
?
I know ++ is a possessive quantifier, but what's the difference in matching the numeric string?
What string can match "\\d+"
but can't with "\\d++"
?
Possessive quantifier seems to be significant with quantifier ".*"
only. Is it true?
Asked
Active
Viewed 2.8k times
10

Pshemo
- 122,468
- 25
- 185
- 269

user2313900
- 201
- 1
- 3
- 8
2 Answers
12
Possessive quantifiers will not back off, even if some backing off is required for the overall match.
So, for example, the regex \d++0
can never match any input, because \d++
will match all digits, including the 0
needed to match the last symbol of the regex.

erickson
- 265,237
- 58
- 395
- 493
3
\d+ Means:
\d
means a digit (Character in the range 0-9), and +
means 1 or more times. So, \d+
is 1 or more digits.
\d++ Means from Quantifiers
This is called the possessive quantifiers and they always eat the entire input string, trying once (and only once) for a match. Unlike the greedy quantifiers, possessive quantifiers never back off, even if doing so would allow the overall match to succeed.

Sumit Singh
- 15,743
- 6
- 59
- 89
-
The question is about `\d++`. – Blender Apr 24 '13 at 04:43
-
1The quote in your answer is incorrect, and it can't be found on the page you linked to. Possessive quantifiers don't always "eat the entire input string". – Tim Pietzcker Sep 22 '14 at 05:19