Regex · <code>*</code> — zero or more
Quantifiers
Quantifiers repeat the previous atom: * zero or more, + one or more, ? optional, and {n,m} an explicit count.
Key facts
| Star | <code>*</code> — zero or more |
|---|---|
| Plus | <code>+</code> — one or more |
| Question mark | <code>?</code> — zero or one |
| Count | <code>{n}</code> — exactly n |
| Range | <code>{n,m}</code> — between n and m, inclusive |
| Open range | <code>{n,}</code> — at least n |
| Applies to | The single preceding atom |
| Default | Greedy |
What the quantifier binds to
A quantifier applies to one atom: a literal character, a class, a group or an escape. ab+ means “a” followed by one or more “b”, not one or more repetitions of “ab”. Parentheses change the scope: (ab)+ repeats the pair.
Alternation has lower precedence than concatenation, so ^cat|dog$ means “starts with cat” or “ends with dog”, not the line containing either word. Group the alternatives when the anchors should apply to both.
Counts and zero-length matches
{0,3} permits zero occurrences, so a group quantified that way can match an empty string. That is legal and occasionally useful, but it complicates global matching: an empty match advances the search position by one character in most engines to avoid an infinite loop.
Very large counts are capped or rejected by some engines, and a quantified group that can match emptiness is a common source of backtracking blow-ups. Prefer a precise lower bound such as {1,64} when validating input.
Frequently asked questions
What does a+ match?
One or more consecutive “a” characters. The match is greedy, so it takes as many as it can while still allowing the rest of the pattern to match.
Is {n,m} inclusive?
Yes. {2,4} matches two, three or four repetitions.
Why does (ab)+ behave differently from ab+?
The group makes “ab” a single atom, so the plus repeats the pair rather than only the b.