Advertisement

Basic Quantifiers

Specify how many times to match.

*           # 0 or more times
+           # 1 or more times
?           # 0 or 1 time (optional)
{n}         # Exactly n times
{n,}        # n or more times
{n,m}       # Between n and m times

Zero or More (*)

Match zero or more occurrences.

a*          # "", "a", "aa", "aaa"
\d*         # Any number of digits
[a-z]*      # Any number of lowercase letters
colou*r     # "color" or "colour"

One or More (+)

Match one or more occurrences.

a+          # "a", "aa", "aaa" (not "")
\d+         # One or more digits
[a-z]+      # One or more lowercase letters
https?      # "http" or "https"
Advertisement

Zero or One (?)

Make pattern optional.

colou?r # "color" or "colour" https? # "http" or "https" \d? # Optional digit -?\d+ # Optional negative sign

Exact Count {n}

Match exactly n times.

\d{3}       # Exactly 3 digits
[a-z]{5}    # Exactly 5 lowercase letters
\w{10}      # Exactly 10 word characters
[0-9]{4}    # Exactly 4 digits (year)

Range {n,m}

Match between n and m times.

\d{2,4}     # 2 to 4 digits
[a-z]{3,6}  # 3 to 6 lowercase letters
\w{1,10}    # 1 to 10 word characters

At Least {n,}

Match n or more times.

\d{3,}      # 3 or more digits
[a-z]{5,}   # 5 or more lowercase letters
.{10,}      # 10 or more any characters

Greedy vs Lazy

Quantifiers are greedy by default.

.*          # Greedy: matches as much as possible
.*?         # Lazy: matches as little as possible
.+?         # Lazy one or more
.{2,5}?     # Lazy range

Common Examples

Practical quantifier usage.

\d{3}-\d{4}          # Phone: 123-4567
[a-z]{2,20}          # Username 2-20 chars
\d{1,3}\.\d{1,3}\.   # IP address start
\w+@\w+\.\w+         # Simple email
Last updated: January 2026
Advertisement