An answer from another question piqued my curiosity.
Consider:
$string = "asfasdfasdfasdfasdf[[sometextomatch]]asfkjasdfjaskldfj";
$regex = "/\[\[(.+?)\]\]/";
preg_match($regex, $string, $matches);
$regex = "/\[\[(.*)\]\]/";
preg_match($regex, $string, $matches);
I asked what the difference between the two regexes is. The aswer I got was that “.*” matches any character 0 or more times as many times as possible,
and “.+?” matches any character 1 or more times as few times as possible.
I read those regexes differently so I did some experimenting on my own but didn’t come to any conclusions. Php.net says “?” is equivalent to {0,1} so you could rewrite
"/\[\[(.+?)\]\]/"
as
"/\[\[((.+){0,1})\]\]/"
or as
"/\[\[(.{0,})\]\]/"
or as
"/\[\[(.*)\]\]/"
Will they capture different text? Is the difference that one is less expensive? Am I being anal?
Just take an example where you get different results:
Your first regular expression will match
[[bar]]and[[quux]]while the second will match only[[bar]] baz [[quux]].The reason for that is that a lazy quantifier (suffixed with
?) will match the minimum of possible repetitions the normal greedy mode will match the maximum of possible repetitions: