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 to get a YouTube video ID?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match special characters?
- How to use PHP regex to match an exact string?
- How to use PHP regex to match a multiline?
- How to use PHP regex to match a year?
- How to use PHP regex to match time?
- How to use regex in PHP to match date in "yyyy-mm-dd" format?
- How to use PHP regex to match an XML tag?
- How to get the first match when using regex in PHP?
See more codes...