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 exactlynoccurrences of the preceding character, group, or character class{n,}- matchesnor more occurrences of the preceding character, group, or character class{n,m}- matches at leastnand at mostmoccurrences of the preceding character, group, or character class
Helpful links
More of Php Regex
- How to use PHP regex with the "x" modifier?
- How to use PHP regex to match an XML tag?
- How to use PHP regex to match whitespace?
- How to use PHP regex to match UUID?
- How to match strings starting with a certain string using PHP regex?
- How to match a double quote in PHP regex?
- How to match a question mark in PHP regex?
- How to use modifiers with PHP regex?
- How to get regex match in PHP?
- How to use greedy regex in PHP?
See more codes...