php-regexHow to use quantifiers in PHP regex?
Quantifiers are used in PHP regex to specify the number of times a character, group, or character class must appear in the input string for a match to be found.
For example, the following code uses the quantifier +
to match one or more occurrences of the letter a
:
$string = 'aaabbb';
preg_match('/a+/', $string, $matches);
The output of the above code will be:
Array
(
[0] => aaa
)
The following is a list of quantifiers and their meanings:
?
- matches zero or one occurrence of the preceding character, group, or character class*
- matches zero or more occurrences of the preceding character, group, or character class+
- matches one or more occurrences of the preceding character, group, or character class{n}
- matches exactlyn
occurrences of the preceding character, group, or character class{n,}
- matchesn
or more occurrences of the preceding character, group, or character class{n,m}
- matches at leastn
and at mostm
occurrences of the preceding character, group, or character class
Helpful links
More of Php Regex
- How to match a space using PHP regex?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match a zip code?
- How to get the first match when using regex in PHP?
- How to match the end of a string when using regex in PHP?
- How to use PHP regex to match an exact string?
- How to use PHP regex to match special characters?
- How to use PHP regex with the "x" modifier?
- How to use PHP regex to match UUID?
- How to match strings starting with a certain string using PHP regex?
See more codes...