9951 explained code solutions for 126 technologies


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 case a.
  • $matches - This is the array that will contain the matches found by the regular expression.

Helpful links

Edit this code on GitHub