3. Quantifiers
Controlling repetition — greedy, lazy, and possessive
So far, every element in our patterns has matched exactly one character. Quantifiers let you say "repeat this part N times" — from zero to unlimited.
The Basic Quantifiers
| Quantifier | Meaning |
|---|---|
* |
Zero or more times |
+ |
One or more times |
? |
Zero or one time (optional) |
{n} |
Exactly n times |
{n,} |
n or more times |
{n,m} |
Between n and m times (inclusive) |
* — Zero or More
Pattern
go*d
Matches
gd, god, good, goood, gooood, ...
Why
o* means "zero or more o's." So even gd (zero o's) matches.
+ — One or More
Pattern
go+d
Matches
god, good, goood, ...
Does NOT match
gd
Why
o+ requires at least one o.
? — Zero or One (Optional)
Pattern
colou?r
Matches
color, colour
Why
u? makes the u optional — it can appear zero or one times.
{n} — Exactly N Times
Pattern
\d{4}-\d{2}-\d{2}
Input
Date: 2026-04-24
Matches
Date: 2026-04-24
{n,m} — Between N and M Times
Pattern
\d{1,3}
Input
5 42 789 1234
Matches
5 42 789 1234
Why
The quantifier matches between 1 and 3 digits. 1234 gets split into
123 and 4 because the engine takes the maximum (greedy) and
then starts a new match.
{n,} — N or More Times
Pattern
a{2,}
Input
a aa aaa ba
Matches
a aa aaa ba
Greedy vs. Lazy
By default, quantifiers are greedy — they match as many characters as possible while still allowing the overall pattern to succeed.
Greedy (Default)
Pattern
".+"
Input
She said "hello" and "goodbye"
Matches
She said "hello" and "goodbye"
Why
The .+ grabs as much as it can — from the first " all the
way to the last ".
Lazy (Add ?)
Append ? to any quantifier to make it lazy — it matches as few characters as
possible.
| Greedy | Lazy |
|---|---|
* |
*? |
+ |
+? |
? |
?? |
{n,m} |
{n,m}? |
Pattern
".+?"
Input
She said "hello" and "goodbye"
Matches
She said "hello" and "goodbye"
Why
The .+? takes the minimum — it stops at the first closing "
it finds.
This is one of the most common regex pitfalls. When you want to match the contents
of delimiters (quotes, tags, brackets), lazy quantifiers or negated character classes
([^"]+) are your friends.
When Negated Classes Are Better
Instead of lazy .+?, you can use a negated character class to achieve the same result
without backtracking:
Pattern
"[^"]+"
Input
She said "hello" and "goodbye"
Matches
She said "hello" and "goodbye"
[^"]+ says "one or more characters that are not a quote." This is often faster than
lazy matching because the engine doesn't need to backtrack — it advances until it sees a
quote, and that's the end.
Possessive Quantifiers (Advanced)
Some engines (Java, PCRE) support possessive quantifiers — append + to the
quantifier. A possessive quantifier is greedy but never gives back characters once matched.
| Greedy | Possessive |
|---|---|
* |
*+ |
+ |
++ |
? |
?+ |
{n,m} |
{n,m}+ |
Pattern
\d++\.\d++
Why use it?
If the digits before the dot fail to find a dot, the engine doesn't backtrack into the digit match. It fails immediately. This can dramatically speed up patterns that would otherwise backtrack catastrophically.
Possessive quantifiers are a performance tool. They don't change what matches when a match exists — they change how fast the engine gives up when a match doesn't exist.
Quantifiers Apply to the Previous Element
A quantifier attaches to the element immediately before it. This is important to understand:
Pattern
abc+
What it means
ab followed by one or more cs. NOT "abc" repeated.
Matches
abc, abcc, abccc, ...
Does NOT match
abcabc
To repeat the entire string "abc", you need a group: (abc)+. We'll cover groups in a
later article.
Key Takeaways
*= zero or more,+= one or more,?= zero or one.{n},{n,},{n,m}give precise control over repetition counts.- Greedy (default) matches as much as possible; lazy (
?suffix) matches as little as possible. - For delimited content, negated character classes (
[^"]+) are often better than lazy quantifiers. - Possessive quantifiers (
++,*+) prevent backtracking — useful for performance. - Quantifiers attach to the single element before them, not to the whole preceding pattern.
Next up: anchors and boundaries — how to tie matches to specific positions in the text.