Skip to main content
Skip table of contents

Quantifiers

LAST UPDATED: DECEMBER 23, 2025

So far, each pattern has matched an exact number of characters. Quantifiers change this by allowing a character, character class, or grouped pattern immediately before it to repeat. This repetition does not change which characters are matched or where matching occurs.

Symbol Quantifiers

Symbol quantifiers describe repetition using a single symbol.

Zero or More

The * quantifier allows the preceding element to appear zero or more times.

Examples - Zero or More

EXAMPLE 1

Input:

CODE
{{ ' ' | regex_match('[A-Za-z0-9]*') }}
{{ "" | regex_match('[A-Za-z0-9]*') }}
{{ 'D3' | regex_match('[A-Za-z0-9]*') }}

Return Data:

CODE
True
True
True

EXAMPLE 2

Input:

CODE
{{ 'report' | regex_match('[A-Za-z]*\.pdf') }}

Return Data:

CODE
False

Explanation: .pdf is required at the end of the match but the input does not contain it.

EXAMPLE 3

Input:

CODE
{{ 'User' | regex_match('[0-9][A-Za-z]*') }}

Return Data:

CODE
False

Explanation: A decimal digit as the first character is required but missing.

EXAMPLE 4

Input:

CODE
{{ '   D3' | regex_match('^[^ \t\n\r\f\v]+') }}
{{ '   D3' | regex_match('^\S+') }}

Return Data:

CODE
False
False

Reminder: \S is equivalent to the character class [^ \t\n\r\f\v].

One or More

The + quantifier allows the preceding element to appear one or more times.

Examples - One or More

EXAMPLE 1

Input:

CODE
{{ 'https://example.com/api/v1' | regex_match('\.com(/[\w.-]+)+') }}
{{ 'https://example.com/download/report.pdf' | regex_match('\.com(/[\w.-]+)+') }}
{{ 'https://example.com/release-notes' | regex_match('\.com(/[\w.-]+)+') }}
{{ 'https://example.com/' | regex_match('\.com(/[\w.-]+)+') }}
{{ 'https://example.com' | regex_match('\.com(/[\w.-]+)+') }}

Reminder: [\w.-] is equivalent to [A-Za-z0-9_.-].

Return Data:

CODE
True
True
True
False
False

EXAMPLE 2

Input:

CODE
{{ '8.8.8.8' | regex_match('[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+') }}
{{ '8.8.8.8' | regex_match('\d+\.\d+\.\d+\.\d+') }}

Reminder: \d is equivalent to the character class [0-9].

Return Data:

CODE
True
True

EXAMPLE 3

Input:

CODE
{{ '_' | regex_match('[A-Za-z0-9_]+') }}
{{ '_' | regex_match('\w+') }}

Reminder: \w is equivalent to the character class [A-Za-z0-9_].

Return Data:

CODE
True
True

EXAMPLE 4

Input:

CODE
{{ '@' | regex_match('[^A-Za-z0-9_]+') }}
{{ '@' | regex_match('\W+') }}

Return Data:

CODE
True
True

Reminder: \W is equivalent to the character class [^A-Za-z0-9_].

Zero or One

The ? quantifier allows the preceding element to appear zero or one time.

Examples - Zero or One

EXAMPLE 1

Input:

CODE
{{ '192.168.200.1' | regex_match('192\.168\.200\.\d?\d?') }}
{{ '192.168.200.10' | regex_match('192\.168\.200\.\d?\d?') }}
{{ '192.168.200.100' | regex_match('192\.168\.200\.\d?\d?') }}

Reminder: \d is equivalent to the character class [0-9].

Return Data:

CODE
True
True
True

Reminder: The pattern is required to match only what it specifies, not the entire input string.

EXAMPLE 2

Input:

CODE
{{ 'demo.url.com' | regex_match('demo\.?\.url\.com') }}
{{ 'demourl.com' | regex_match('demo\.?\.url\.com') }}

Return Data:

CODE
True
False

EXAMPLE 3

Input:

CODE
{{ 'https://docs.d3security.com' | regex_match('\.com/?') }}
{{ 'https://docs.d3security.com/' | regex_match('\.com/?') }}

Return Data:

CODE
True
True

EXAMPLE 4

Input:

CODE
{{ 'https://docs.d3security.com' | regex_match('^(https://)?') }}
{{ 'docs.d3security.com' | regex_match('^(https://)?') }}

Return Data:

CODE
True
True

Counted Quantifiers

Counted quantifiers specify repetition using numbers instead of symbols. They provide precise control over how many times an element may appear.

Exact Repetition

The {m} quantifier requires the preceding element to appear exactly m times.

Examples - Exact Repetition

EXAMPLE 1

Input:

CODE
{{ '0' | regex_match('.{2}') }}
{{ '00' | regex_match('.{2}') }}
{{ '000' | regex_match('.{2}') }}

Return Data:

CODE
False
True
True

EXAMPLE 2

Input:

CODE
{{ 'XY' | regex_match('X[A-Z]{2}') }}
{{ 'XYZ' | regex_match('X[A-Z]{2}') }}

Return Data:

CODE
False
True

EXAMPLE 3

Input:

CODE
{{ '192.168.200.1' | regex_match('192\.168\.200\.(1[0-9]{2}|2[0-4][0-9])') }}
{{ '192.168.200.99' | regex_match('192\.168\.200\.(1[0-9]{2}|2[0-4][0-9])') }}
{{ '192.168.200.100' | regex_match('192\.168\.200\.(1[0-9]{2}|2[0-4][0-9])') }}
{{ '192.168.200.249' | regex_match('192\.168\.200\.(1[0-9]{2}|2[0-4][0-9])') }}
{{ '192.168.200.250' | regex_match('192\.168\.200\.(1[0-9]{2}|2[0-4][0-9])') }}

Explanation: Group (1[0-9]{2}|2[0-4][0-9]) matches integers from 100 through 249 inclusive.

Return Data:

CODE
False
False
True
True
False

EXAMPLE 4

Input:

CODE
{{ '3C:7A:2F:91:B4:6D' | regex_match('([A-Fa-f0-9]{2}:){5}[A-Fa-f0-9]{2}') }}
{{ 'AA:1B:2C:3D:4E:5G' | regex_match('([A-Fa-f0-9]{2}:){5}[A-Fa-f0-9]{2}') }}
{{ '9F:4F:C2:7D:18:E3' | regex_match('([A-Fa-f0-9]{2}:){5}[A-Fa-f0-9]{2}') }}

Return Data:

CODE
True
False
True

Bounded Repetition

The {m,n} quantifier requires the preceding element to appear at least m times and at most n times.

Examples - Bounded Repetition

EXAMPLE 1

Input:

CODE
{{ '0' | regex_match('0{2,3}') }}
{{ '00' | regex_match('0{2,3}') }}
{{ '000' | regex_match('0{2,3}') }}
{{ '001' | regex_match('0{2,3}') }}
{{ '001' | regex_match('0{2,3}$') }}

Return Data:

CODE
False
True
True
True
False

EXAMPLE 2

Input:

CODE
{{ 'D3-01' | regex_match('\w{2,11}-\d{2,4}$') }}
{{ 'D3-00001' | regex_match('\w{2,11}-\d{2,4}$') }}
{{ 'd3_security-0001' | regex_match('\w{2,11}-\d{2,4}$') }}

Return Data:

CODE
True
False
True

EXAMPLE 3

Input:

CODE
{% set potential_key1 = 'sk-' ~ 'proj-' ~ 'a' * 156 %}
{% set potential_key2 = 'sk-' ~ 'svcacct-' ~ 'b' * 156 %}
{% set potential_key3 = 'sk-' ~ 'c' * 160 %}
{% set potential_key4 = 'sk-' ~ 'd' * 165 %}

{{ potential_key1 | regex_match('^sk-[\w-]{161,164}$') }}
{{ potential_key2 | regex_match('^sk-[\w-]{161,164}$') }}
{{ potential_key3 | regex_match('^sk-[\w-]{161,164}$') }}
{{ potential_key4 | regex_match('^sk-[\w-]{161,164}$') }}

Explanation: ~ is the string concatenation operator, and * is the string repetition operator.

Return Data:

CODE
True
True
False
False

EXAMPLE 4

Input:

CODE
{{ 'jdoe@demo.com' | regex_match('^[A-Za-z]{2,10}@demo\.com') }}
{{ 'usr7@demo.com' | regex_match('^[A-Za-z]{2,10}@demo\.com') }}
{{ 's@demo.com' | regex_match('^[A-Za-z]{2,10}@demo\.com') }}
{{ 'longlocalpart@demo.com' | regex_match('^[A-Za-z]{2,10}@demo\.com') }}

Return Data:

CODE
True
False
False
False

Minimum Repetition

The {m,} quantifier requires the preceding element to appear at least m times, with no upper limit.

Examples - Minimum Repetition

EXAMPLE 1

Input:

CODE
{{ '0' | regex_match('.{2,}') }}
{{ '00' | regex_match('.{2,}') }}
{{ '000' | regex_match('.{2,}') }}

Return Data:

CODE
False
True
True

EXAMPLE 2

Input:

CODE
{{ 'https://demo.com/api/v1'| regex_match('\.com(/[\w.-]{1,}){1,}') }}
{{ 'https://demo.com/report.pdf' | regex_match('\.com(/[\w.-]{1,}){1,}') }}
{{ 'https://demo.com/release-notes' | regex_match('\.com(/[\w.-]{1,}){1,}') }}
{{ 'https://demo.com/' | regex_match('\.com(/[\w.-]{1,}){1,}') }}
{{ 'https://demo.com' | regex_match('\.com(/[\w.-]{1,}){1,}') }}

Return Data:

CODE
True
True
True
False
False

EXAMPLE 3

Input:

CODE
{% set url1='https://docs.d3security.com' %} 
{% set url2='www.docs.d3security.com' %}
{% set url3='docs.d3security.com' %}
{% set url4='d3security.com' %}

{{ url1 | regex_match('[\w]{1,}(\.[\w]{1,}){1,}\.com') }}
{{ url2 | regex_match('[\w]{1,}(\.[\w]{1,}){1,}\.com') }}
{{ url3 | regex_match('[\w]{1,}(\.[\w]{1,}){1,}\.com') }}
{{ url4 | regex_match('[\w]{1,}(\.[\w]{1,}){1,}\.com') }}

Return Data:

CODE
True
True
True
False

EXAMPLE 4

Input:

CODE
{% set str1='Lorem ipsum dolor sit amet' %}
{% set str2='Lorem  ipsum dolor sit amet' %}
{% set str3='Lorem ipsum dolor   sit amet' %}
{% set str4='Lorem ipsum dolor sit amet    ' %}

{{ str1 | regex_match('[A-Za-z]{1,}\s{2,}[A-Za-z]{1,}') }}
{{ str2 | regex_match('[A-Za-z]{1,}\s{2,}[A-Za-z]{1,}') }}
{{ str3 | regex_match('[A-Za-z]{1,}\s{2,}[A-Za-z]{1,}') }}
{{ str4 | regex_match('[A-Za-z]{1,}\s{2,}[A-Za-z]{1,}') }}

Explanation: The last one returns false because the pattern requires letters on both sides of a whitespace run of at least two characters (\s{2,}), and the trailing whitespace in str4 is not followed by letters.

Return Data:

CODE
False
True
True
False

FAQs

What does the string concatenation (~) operator do?

The ~ operator joins two or more string values into a single string.

EXAMPLE 1

Input:

CODE
{{ 'D' ~ '3' }}

Return Data:

CODE
D3

EXAMPLE 2

Input:

CODE
{% set value='sk-' ~ 'proj-' %}
{{ value }}

Return Data:

CODE
sk-proj-
What does the string repetition (*) operator do?

The * operator repeats a string value a specified number of times to produce a new string.

EXAMPLE 1

Input:

CODE
{% set value = 'D' * 3 %}
{{ value }}

Return Data:

CODE
DDD

EXAMPLE 2

Input:

CODE
{% set value = 'xyz' * 2 %}
{{ value }}

Return Data:

CODE
xyzxyz
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.