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 match a year?
- How to use PHP regex to match an exact string?
- How to use PHP regex to match UUID?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match an XML tag?
- How to use PHP regex to match URL path?
- How to match strings starting with a certain string using PHP regex?
- How to match a space using PHP regex?
- How to remove non-printable characters using PHP regex?
- How to match a single quote in PHP regex?
See more codes...