3

+? Matches the previous atom one or more times, while consuming as little input as possible.

I don't understand this,can someone help me?

serenesat
  • 4,611
  • 10
  • 37
  • 53
Robin
  • 543
  • 1
  • 6
  • 12
  • 1
    http://stackoverflow.com/questions/2301285/what-do-lazy-and-greedy-mean-in-the-context-of-regular-expressions – xxfelixxx Aug 14 '15 at 09:51

2 Answers2

2

(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?

Community
  • 1
  • 1
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
0

+ 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

serenesat
  • 4,611
  • 10
  • 37
  • 53