Regex Quantifiers
Quantifier Constructs
Quantifiers specify how often the preceding regular expression construct should match.
The classes of quantifiers are:
- Greedy
- Reluctant
- Possessive
The difference between greedy, reluctant, and
possessive quantifiers involves what part of the string to try for the initial match, and how
to retry if the initial attempt doesn't produce a match.
By default, quantifiers are
greedy
. A greedy quantifier first tries for a match
with the entire input string. If that produces a match, then it considers the match a
success and the engine can move on to the next construct in the regular expression. If
the first try doesn't produce a match, the engine backs-off 1 character at a time until
it finds a match. So a greedy quantifier checks for possible matches in order from the
longest possible input string to the shortest possible input string, recursively trying
from right to left.Adding a
?
(question mark) to a greedy quantifier makes it
reluctant
. A reluctant quantifier first tries for a match from the beginning
of the input string, starting with the shortest possible piece of the string that
matches the regex construct. If that produces a match, then it considers the match a
success and the engine can move on to the next construct in the regular expression. If
the first try doesn't produce a match, the engine adds 1 character at a time until it
finds a match. So a reluctant quantifier checks for possible matches in order from the
shortest possible input string to the longest possible input string, recursively trying
from left to right. Adding a
+
(plus sign) to a greedy quantifier makes it
possessive
. A possessive quantifier is like a greedy quantifier on the first
attempt (it tries for a match with the entire input string). The difference is that
unlike a greedy quantifier, a possessive quantifier doesn't retry a shorter string if it
doesn't find a match. If the initial match fails, the possessive quantifier reports a
failed match. It doesn't make any more attempts.Greedy Construct | Reluctant Construct | Possessive Construct | Description | Example |
|---|---|---|---|---|
?
| ??
| ?+
| Matches the previous character or construct once or not at all. | st?on matches "son" in "johnson" and "ston" in "johnston"
but nothing in "clinton" or "version" |
*
| *?
| *+
| Matches the previous character or construct zero or more times. | if* matches "if", "iff" in "diff", or "i" in "print" |
+
| +?
| ++
| Matches the previous character or construct 1 or more times. | if+ matches "if", "iff" in "diff", but nothing in "print" |
{n}
| {n}?
| {n}+
| Matches the previous character or construct exactly n
times. | o{2} matches "oo" in "lookup" and the first 2 o's in
"fooooo" but nothing in "mount" |
{n,}
| {n,}?
| {n,}+
| Matches the previous character or construct at least n
times. | o{2,} matches "oo" in "lookup" all 5 o's in "fooooo" but
nothing in "mount" |
{n,m}
| {n,m}?
| {n,m}+
| Matches the previous character or construct at least n times, but no
more than m times. | F{2,4} matches "FF" in "#FF0000" and the last 4 F's in
"#FFFFFF" |