Quantifiers
Unless you say otherwise, each item in a regular expression matches
just once. With a pattern like /nop/,
each of those characters must match, each right after the other. Words
like “panoply” or “xenophobia” are fine, because
where the match occurs doesn’t matter.
If you wanted to match both “xenophobia” and “Snoopy”, you couldn’t use the /nop/
pattern, since that requires just one “o” between the “n” and the “p”, and Snoopy has two. This is where quantifiers come in handy: they say how
many times something may match, instead of the default of matching just
once. Quantifiers in a regular expression are like loops in a program; in
fact, if you think of a regex as a program, then they
are loops. Some loops are exact, like “repeat this
match only five times” ({5}). Others
give both lower and upper bounds on the match count, like “repeat this
match at least twice but no more than four times” ({2,4}). Others have no closed upper bound at
all, like “match this at least twice, but as many times as you’d like”
({2,}).
Table 5-17 shows the quantifiers that Perl recognizes in a pattern.
Table 5-17. Regex quantifiers compared
| Maximal | Minimal | Possessive | Allowed Range |
|---|---|---|---|
{ | { | { | Must occur at least
MIN times but no more than
MAX times |
{ | { | { | Must occur at least
MIN times |
{ | { | { | Must match exactly
COUNT times |
* | *? | *+ | 0 or more times (same as {0,}) |
+ | +? | ++ | 1 or more times (same as {1,}) |
? | ?? | ?+ | 0 or 1 time (same as {0,1}) |
Something with a * or a ? doesn’t ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access