I have noticed some strange behaviour with a PCRE regular expression I can't explain. I would expect the code:
preg_match('!^.+?(?:/programs/([^?#]+))?.*?$!',
'http://example.com/programs/drive', $matches);
to return "drive" as match 1. The [^?#]+ and the ? after the non-capturing group are both greedy so why doesn't the [^?#]+ take precedence and match drive? Instead testing revealed that the .+? at the start matches the h and the .*? at the end matches the rest of the URL.
By contrast, the code:
preg_match('!^.+?(?:/programs/([^?#]+).*)?$!',
'http://example.com/programs/drive', $matches);
works as expected and returns drive as match 1.