2

I was browsing old php sources and I found a pattern I don't understand (probably a copy/past from the internet some times ago ...).

Here is a simple exemple using it with php :

echo preg_replace('#color="(.+)"#', '$1', 'color="red" color="black"');
// Echo 'red" color="black', which is fine because the (.+) try to match the largest possible string.

echo preg_replace('#color="(.+?)"#', '$1', 'color="red" color="black"');
// Echo 'red black', why ? There is some black magic behind '(.+?)' I don't understand !

So what does the '?' do in '(.+?)' ? I assume it says something like 'don't match the rest of the regex' but I'm looking for a detailed explanation !

Lynn Crumbling
  • 12,985
  • 8
  • 57
  • 95
dsagilles
  • 57
  • 1
  • 8
  • 1
    `?` after `+` forces the regex engine to do a shortest possible match (non-greedy or reluctant). – Avinash Raj Aug 27 '14 at 13:29
  • It is a reluctant quantifier. It looks for the smallest sequence that match the `.+`. More info : http://stackoverflow.com/questions/5319840/greedy-vs-reluctant-vs-possessive-quantifiers – Aserre Aug 27 '14 at 13:31
  • 1
    possible duplicate of [Reference - What does this regex mean?](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – Avinash Raj Aug 27 '14 at 13:31
  • Thanks, that was the information I was missing. It's very clear now. – dsagilles Aug 27 '14 at 13:54

2 Answers2

2

+ is a greedy operator; consuming as much as possible. Therefore, .+ will match as much as it can and still allow the remainder of the regular expression to match. Once you specify the question mark +?, you're telling the regex engine (don't be greedy.. as soon as you find a double quote "... stop, you're done.)

hwnd
  • 69,796
  • 4
  • 95
  • 132
1

The ? makes the match non-greedy, meaning that the expression .+? will match as few characters as possible to make the regex match, rather than matching as any as possible, which is the default behaviour.

RichieHindle
  • 272,464
  • 47
  • 358
  • 399