There is a moment in our regular expressions that we need to determinate the amount of times a character or a group should appear. For that, the regex engines offer us the feature of quantifiers. In the next sections we are going to see the types of quantifiers that we can use.

Zero or more

This quantifier we use when we have a character in our regular expression that could appear zero, one or multiple times. In order to express that, we need to put a asterisk symbol (*) right after the character or the group we want the rule to be applied to. For example:

Regex:

go*d

Matches:

gd
god
good
goooood

In the previous example, we applied the zero-or-more quantifier to the character o. See that all cases were matched because every one of them fits the rule.

One or more

This quantifier, like the name suggests, it matches one or multiple occurrences of a determinated character or group. In order to use that quantifier, we need to put a plus sign (+) right after the character or the group we want the rule to be applied to. See the example below:

Regex:

ba+d

Matches:

bd
bad
baad
baaaaad

In this example, the first case didn't match because there wasn't at least 1 character a between b and d. The others matched normally.

Zero or one

This quantifier is used when we want to specify that a character is not mandatory. There could and there could not be occurrences of that character. For that, we use the question mark (?) the same way we did with the other quantifiers. Example:

Regex:

ab?c

Matches:

ac
abc
abbbc

This example is very simple. The only two cases that could have matched were the first ones. The last one didn't matched because there was more than one b, then the quantifier doesn't allow that amount.

Exact number of times

We can also specify a fixed number of times that we want a character or group to appear. In order to do that, we need to use the syntax ({n}) right after the character or group we want to repeat, where n is the number of times we want that expression to appear. See the example below:

Regex:

\d{3}\.\d{3}\.\d{3}-\d{2}

Matches:

345.723.786-14
34.723.86-14
.723.736-1435

In the example above, we created a simple regular expression to match valid brazilian CPFs. Notice that we determinated the amount of digits that should appear in each part of the expression.

Range

Another useful feature in quantifiers is the possibility to specify ranges. In other words, determinate the minimum and maximum amount of times a character or group should appear. To do that, we need to use the syntax ({n,m}) right after the character or group we want to repeat, where n is the minimum amount and m is the maximum amount of times the character or group should appear. Example:

Regex:

\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}

Matches:

44.22.666.23
1.6.9000.100
225.225.225.0
.1.49.1111

This example is straightforward, we set a rule to match ip addresses. They should have at least 1 digit and no more than 3 in each layer. The second and fourth case didn't match because they have more than 3 digits in one layer, and the last one doesn't have a digit at the beginning.

At least n times

This quantifier is just like the one-or-more, the only difference is that in this one we can specify the minimum amount. In order to do that, we use the same sintax of range, but we do not put a m value. So the syntax sould be like ({n,}). See the example:

Regex:

say( hey){2,} to me

Matches:

say hey to me
say hey hey to me
say hey hey hey to me
say hey hey hey hey hey to me

This time we applied the quantifier to a group. Basically, in the example above we want the word hey to appear at least two times, and there is a unlimited time that it can appear after it.