Regex plus vs star difference?

They are called quantifiers.

* 0 or more of the preceding expression

+ 1 or more of the preceding expression

Per default a quantifier is greedy, that means it matches as many characters as possible.

The ? after a quantifier changes the behaviour to make this quantifier "ungreedy", means it will match as little as possible.

Example greedy/ungreedy

For example on the string "abab"

a.*b will match "abab" (preg_match_all will return one match, the "abab")

while a.*?b will match only the starting "ab" (preg_match_all will return two matches, "ab")

You can test your regexes online e.g. on Regexr, see the greedy example here


+ matches at least one character

* matches any number (including 0) of characters

The ? indicates a lazy expression, so it will match as few characters as possible.


The first (+) is one or more characters. The second (*) is zero or more characters. Both are non-greedy (?) and match anything (.).

Tags:

Php

Regex