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

MaximalMinimalPossessiveAllowed Range
{MIN,MAX}{MIN,MAX}?{MIN,MAX}?+Must occur at least MIN times but no more than MAX times
{MIN,}{MIN,}?{MIN,}?+Must occur at least MIN times
{COUNT}{COUNT}?{COUNT}?+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 ...

Get Programming Perl, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.