Coder Perfect

What’s the difference between Regex plus and Star? [duplicate


What’s the distinction between:




when I use preg match regex in php?

Asked by David19801

Solution #1

Quantifiers are what they’re called.

* at least one of the previous expressions

+ one or more of the expressions before it

A quantifier is greedy by default, which means it matches as many characters as feasible.

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

Example greedy/ungreedy

For instance, consider the string “abab.”

“abab” will be matched by a.*b (preg match all will only yield one match, “abab”).

while a.*?b will only match the first “ab” (preg match all will yield two matches, both “ab”),

You may put your regexes to the test online, for example, on Regexr (see the greedy example here).

Answered by stema

Solution #2

One or more characters make up the first (+). The second (*) indicates that there are zero or more characters. They’re both non-greedy (?) and go with anything (.).

Answered by Quentin

Solution #3

+ is a character that matches at least one other character.

* matches any number of characters (including 0).

The? denotes a lazy expression, which means it will match the fewest characters possible.

Answered by Xophmeister

Solution #4

A + is a pattern that matches one or more instances of the previous pattern. A * corresponds to one or more instances of the previous pattern.

So, if you use a +, the pattern must have at least one instance, however if you use a *, the pattern will match even if there are none.

Answered by DaveRandom

Solution #5

Consider the string to be matched below.


The pattern (ab.*) will return a match for the capture group ab.

While the pattern (ab.+) will not match, nothing will be returned.

However, if you alter the string to aba for pattern (ab.+), it will return aba.


Answered by Azri Jamil

Post is based on