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 five times only" (`{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.12 shows the quantifiers that Perl recognizes in a pattern.

Table 5-12. Regex Quantifiers Compared

Maximal | Minimal | Allowed Range |
---|---|---|

`{` ,`MIN` `MAX` `}` | `{` ,`MIN` `MAX` `}?` | Must occur at least
times but no more than `MIN`
times`MAX` |

`{` `MIN` `,}` | `{` `MIN` `,}?` | Must occur at least
times`MIN` |

`{` `COUNT` `}` | `{` `COUNT` `}?` | Must match exactly
times`COUNT` |

`*` | `*?` | 0 or more times (same as |

`+` | `+?` | 1 or more times (same as
`{1,}` ) |

`?` | `??` | 0 or 1 time (same as
`{0,1}` ) |

Something with a `*`

or a `?`

doesn't actually have to match. That's because they ...

Start Free Trial

No credit card required