Thanks Geert. The two items
?: and
? are not related. When you specify a group such as
(abc), that group is returned as part of the matches array -- it captures whatever is inside as a special group. If you use
(?:abc) then the group becomes non-capturing, which simply means that the group is not returned with the matches.
For those who like code snippets, here's what happens:
php Code:
preg_match('/t(es)t/',
'test',
$matches);
/*
Array
(
[0] => test
[1] => es
)
*/preg_match('/t(?:es)t/',
'test',
$matches);
/*
Array
(
[0] => test
)
*/
In extension to Geert's post above, the
U (PCRE_UNGREEDY) modifier can be used at the end of the regex in order to reverse the normal behaviour. By default, patterns are greedy but with the U modifier they will become non-greedy. In this case, if the
? is used (eg.
(.*?) it will make the preceeding quantifier greedy. That's probably confusing to deal with at the moment so I just mention it as an aside.