PHP Regex Quantifiers (original) (raw)

Summary: in this tutorial, you’ll learn how to use quantifiers in regular expressions to match a number of instances of a character or a character class.

Introduction to regex quantifiers #

Quantifiers allow you to match their preceding elements a number of times. The following table shows the list of quantifiers:

Quantifier Meaning
* Match zero or more times.
+ Match one or more times.
? Match zero or one time.
{ n } Match exactly n times.
{ n ,} Match at least n times.
{ n , m } Match from n to m times.

Match zero or more times (*) #

The * quantifier matches its preceding element zero or more times.

The following example uses the * quantifier to match text that ends with PHP:

`<?php

$pattern = '/\w*PHP/'; $title = 'CakePHP & FuelPHP are PHP Frameworks';

if (preg_match_all($pattern, title,title, title,matches)) { print_r($matches[0]); }`Code language: PHP (php)

Try it

Output:

Array ( [0] => CakePHP [1] => FuelPHP [2] => PHP )Code language: PHP (php)

Match one or more times (+) #

The + quantifier matches its preceding element one or more times. For example, the \d+ matches one or more digits.

The following example uses the + quantifier to match one or more digits in a string:

`<?php

$pattern = '/\d+/'; $title = 'PHP 1.0 was released in 1995';

if (preg_match_all($pattern, title,title, title,matches)) { print_r($matches[0]); }`Code language: PHP (php)

Try it

Output:

Array ( [0] => 1 [1] => 0 [2] => 1995 )Code language: PHP (php)

Match zero or one time (?) #

The ? quantifier matches its preceding element zero or one time. For example:

`<?php

$pattern = '/behaviou?r/'; $title = 'Which is correct behavior or behaviour?';

if (preg_match_all($pattern, title,title, title,matches)) { print_r($matches[0]); }`Code language: PHP (php)

Try it

Output:

Array ( [0] => behavior [1] => behaviour )Code language: PHP (php)

In this example, the u? matches zero or one character u. Therefore, the matches include both behavior and behaviour

Match Exactly n Times: {n} #

The {n} quantifier matches its preceding element exactly n time, where n is zero or a positive integer.

The following example uses the quantifier {n} to match a time string with the hh:mm format:

`<?php

$pattern = '/\d{2}:\d{2}/'; $title = "It was 12:15 AM";

if (preg_match_all($pattern, title,title, title,matches)) { print_r($matches[0]); }`Code language: PHP (php)

Try it

Output:

Array ( [0] => 12:15 )Code language: PHP (php)

In this example, the \d{2} matches exactly two digits. Therefore, the \d{2}:\d{2} matches two digits, a colon :, and then two digits.

Match at least n times: {n,} #

The {n,} quantifier matches its preceding element at least n time, where n is zero or a positive integer.

The following example uses the {n, } quantifier to match the date strings with the m-d-yyyy or mm-dd-yyyy format:

`<?php

$pattern = '/\d{1,}-\d{1,}-\d{4}/'; $title = "1-1-2020 or 01-01-2020 or 1/1/2020";

if (preg_match_all($pattern, title,title, title,matches)) { print_r($matches[0]); }`Code language: PHP (php)

Try it

Output:

Array ( [0] => 1-1-2020 [1] => 01-01-2020 )Code language: PHP (php)

Match Between n and m Times: {n,m} #

The {n,m} quantifier matches its preceding element at least n times, but no more than m times, where n and m are zero or a positive integer.

For example, the regular expression '/\d{1,}-\d{1,}-\d{1,}/' also matches 1000-1000-2020. To make it more accurate, you can use the {n, m} like this:

`<?php

$pattern = '/\d{1,2}-\d{1,2}-\d{4}/'; $title = "1-1-2020 or 01-01-2020 or 1/1/2020";

if (preg_match_all($pattern, title,title, title,matches)) { print_r($matches[0]); }`Code language: PHP (php)

Try it

In this example, \d{1,2} matches one or two digits. Therefore, the \d{1,2}-\d{1,2}-\d{4} matches d-m-yyyy or dd-mm-yyyy.

Summary #

Did you find this tutorial useful?