php-regexHow to match one or more occurrences in PHP regex?
To match one or more occurrences in PHP regex, the +
quantifier can be used. This quantifier matches one or more of the preceding character, group, or character class.
For example, the following code will match one or more occurrences of the letter a
:
$string = 'aabbcc';
preg_match('/a+/', $string, $matches);
The output of the above code will be:
Array
(
[0] => aa
)
The code consists of the following parts:
$string = 'aabbcc';
- This is the string that will be searched for matches.preg_match('/a+/', $string, $matches);
- This is the regular expression that will be used to search for matches. The+
quantifier is used to match one or more occurrences of the preceding character, in this casea
.$matches
- This is the array that will contain the matches found by the regular expression.
Helpful links
More of Php Regex
- How to use PHP regex with zero or more occurrences?
- How to match a double quote in PHP regex?
- 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 named capture groups in PHP regex?
- How to use PHP regex to match an exact string?
- How to use PHP regex to match a zip code?
- How to use PHP regex with the "x" modifier?
- How to use PHP regex to match whitespace?
- How to use PHP regex to match tab?
See more codes...