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 to match a zip code?
- How to match a quotation mark in PHP regex?
- How to use PHP regex to match a year?
- How to use PHP regex with zero or more occurrences?
- How to use PHP regex to match an exact string?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to get a YouTube video ID?
- How to use PHP regex to match special characters?
- How to use PHP regex to match whitespace?
- How to use PHP regex to match UUID?
See more codes...