Regex Quantifiers – A Complete Guide

What is a regex quantifier?

A regex quantifier specifies the number of consecutive occurrences of the character or expression directly preceding it. Quantifiers can specify zero-or-more (*), one-or-more (+), zero-or-one (?), a specific quantity such as three {3}, more than three {3,}, or between one and three {1,3}. A lazy flag (?) added behind any quantifier will make it match as few characters as possible.

Types of Quantifiers

In Javascript regex there are six types of quantifiers:

1. Zero or More (*Edit with Regexity)

An asterisk (*Edit with Regexity) matches zero-or-more of the preceding character or expression. The expression a*Edit with Regexity will match any number of consecutive occurrences of the letter aEdit with Regexity including no occurrence.

2. One or More (+Edit with Regexity)

A plus sign (+) matches one-or-more of the preceding character or expression. The expression a+Edit with Regexity will match any number of consecutive occurrences, but will not match if there is no occurrence.

3. Zero or One (?Edit with Regexity)

A question mark (?) matches zero-or-one of the preceding character or expression. Therefore it acts like an optional quantifier. For example, the expression https?Edit with Regexity will match both httpEdit with Regexity and httpsEdit with Regexity since the sEdit with Regexity is optional.

4. Specific Quantity x ({x}Edit with Regexity)

A specific number x of occurrences can be specified using curly braces, such as {x}Edit with Regexity. The expression a{3}Edit with Regexity will match exactly 3 occurrences of aEdit with Regexity, no more and no less.

5. x or More ({x,}Edit with Regexity)

To match x or more occurrences, include a comma after x in curly brackets, such as {x,}Edit with Regexity. The expression a{5,}Edit with Regexity will match 5, 6, 7, 8, … up to infinite consecutive occurrences of aEdit with Regexity but will fail for 4 or less occurrences of aEdit with Regexity.

6. Between x and y ({x,y}Edit with Regexity)

The expression {x,y}Edit with Regexity will match between x and y consecutive occurrences, inclusive, of the preceding character or expression. For example, the expression {2,6}Edit with Regexity will match between 2 and 6 occurrences of aEdit with Regexity, inclusive. It will fail if there’s only one occurrence of aEdit with Regexity or more than 6 occurrences.

How to Use a Quantifier

Quantifiers act on the character, group, or character range directly in front of them. For example, the zero-or-one quantifier in abc?deEdit with Regexity will act only on the cEdit with Regexity, and will have no effect on the other characters.

In the expression [12345]{3}Edit with Regexity, the quantifier will allow any 3 consecutive characters from the character group directly in front of it. In the expression (a|b)+Edit with Regexity, the quantifier will allow one or more consecutive instances of the entire capture group.

Quantifiers can also be nested to produce interesting results. For example, the expression (a*b+){2}Edit with Regexity will match two consecutive instances of the capture group’s contents, which in turn will match various permutations of aEdit with Regexity and bEdit with Regexity such as abEdit with Regexity or aabbbEdit with Regexity or simply bbEdit with Regexity.

Tip: A quantifier cannot act on many of the reserved characters found in regex such as an anchor (^Edit with Regexity or $Edit with Regexity), an alternation symbol (|Edit with Regexity), or another quantifier. This will throw an error.

Lazy Quantifiers

By default, quantifiers are greedy – which means they will match as many occurrences as possible. This is especially true for the zero-or-more *Edit with Regexity, one-or-more +Edit with Regexity, and x-or-more {x,}Edit with Regexity quantifiers which allow infinite matching.

For example, when the expression a+Edit with Regexity is used to find matches in the string baaaaEdit with Regexity, it will match all 4 consecutive occurrences of aEdit with Regexity at the same time.

However, this behavior can be altered using the lazy flag ?Edit with Regexity, a question mark added directly following any quantifier, such as a*?Edit with Regexity, a+?Edit with Regexity, a??Edit with Regexity, a{3}?Edit with Regexity, etc. The lazy flag will alter the quantifier’s behavior so that it matches as few consecutive characters as possible which are still allowable.

Returning to the earlier example, if the expression a+?Edit with Regexity (this time with a lazy quantifier) is used to find matches in the string baaaaEdit with Regexity, it will match only one aEdit with Regexity at a time, and thus return 4 separate matches containing one aEdit with Regexity each (assuming the global flag has been turned on).

Conclusion

Quantifiers are one of the basic building blocks in regex and are an important tool in any regexer’s toolbox. In this article, we learned about 6 different types of quantifiers, which characters they act upon, and also how to switch to lazy behavior.

Benjamin

Founder, owner, and sole content creator on RegexLand. Enjoys programming, blogging, and teaching others how to do the same. Read more...

1 thought on “Regex Quantifiers – A Complete Guide”

  1. I have 2 rows.

    1. 43, oli&polygel
    2. oil

    What is the expression to parse both rows into 2 separate columns like

    43 oil&polygel
    oil

    Reply

Leave a Comment