+?
Matches the previous atom one or more times, while consuming as little input as possible.
I don't understand this,can someone help me?
+?
Matches the previous atom one or more times, while consuming as little input as possible.
I don't understand this,can someone help me?
(ab)+?
will try to match ab
which occurs at least once and it will return as soon as it finds the pattern, hence it's lazy.
(ab)+
will try to match all occurrences of ab
which occur at least once and then it will return, hence it's greedy.
Demo: https://regex101.com/r/rC2oB9/1 and https://regex101.com/r/hP7lM9/1
Notice that in first demo first occurrence of ab
was matched (which is highlighted) whereas in second demo all occurrences of ab were matched (highlighted)
Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data.
See: What do lazy and greedy mean in the context of regular expressions?
+
and ?
is the quantifiers.
+
: Match 1 or more times,?
: Match 1 or 0 times,By default, a quantified subpattern is "greedy", that is, it will match as many times as possible (given a particular starting location) while still allowing the rest of the pattern to match. If you want it to match the minimum number of times possible, follow the quantifier with a "?".
Note that the meanings don't change, just the "greediness":
+?
: Match 1 or more times, not greedily.
For more info see this : Quantifiers