PHP & regular experssion named groups
By Rayed
This is a nice regular expression feature, sometime when you have complex regular expression, you could forget what each refer to:
$str = '2005-08-01';
preg_match('/(\d{4})-(\d{1,2})-(\d{1,2})/', $str, $m);
Now you don’t know which group is which part in the regular experssion.
Instead you can name all groups by using the format (?P<name>pattern) instead of just (pattern), then you can access this group by name you assigned it, so the previous regular experssion could be written like:
preg_match('/(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})/', $str, $m);