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 !
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…