Re: [PHP] PHP and named groups in regex/PCRE
Daevid Vincent wrote: > A friend was showing off his regex-fu and the virtues of "named groups" > and how great Python is, knowing that PHP had to be equally as good > (since it uses the same PCRE libraries), we whipped up this little test. > So now us PHP folks can enjoy the same benefit! :-) > > vince...@dev1:~$ cat ./named_groups_test.php > //http://us.php.net/manual/en/function.preg-match.php > //http://www.regular-expressions.info/named.html > > preg_match("/(?Pabc)(.*)(?Pxyz)/", >'abcdefghijklmnopqrstuvwxyz', >$matches); > print_r($matches); > ?> > > vince...@dev1:~$ php ./named_groups_test.php > Array > ( > [0] => abcdefghijklmnopqrstuvwxyz > [foo] => abc > [1] => abc > [2] => defghijklmnopqrstuvw > [bar] => xyz > [3] => xyz > ) > > This needs to be directed to whomever it was about six months ago that was asking about this. They were trying to figure out how to do this, but I believe they were told that it could not be done. Nice example! -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP and named groups in regex/PCRE
On Fri, Mar 6, 2009 at 19:12, Daevid Vincent wrote: > A friend was showing off his regex-fu and the virtues of "named groups" > and how great Python is, knowing that PHP had to be equally as good > (since it uses the same PCRE libraries), we whipped up this little test. > So now us PHP folks can enjoy the same benefit! :-) Appending to Daevid's post, a snippet from the note he posted about this a little bit ago (it will appear on the manual entry soon): Note that you actually get the named group as well as the numerical key value too, so if you do use them, and you're counting array elements, be aware that your array might be bigger than you initially expect it to be. -- daniel.br...@parasane.net || danbr...@php.net http://www.parasane.net/ || http://www.pilotpig.net/ 50% Off All Shared Hosting Plans at PilotPig: Use Coupon DOW1 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] PHP and named groups in regex/PCRE
A friend was showing off his regex-fu and the virtues of "named groups" and how great Python is, knowing that PHP had to be equally as good (since it uses the same PCRE libraries), we whipped up this little test. So now us PHP folks can enjoy the same benefit! :-) vince...@dev1:~$ cat ./named_groups_test.php http://us.php.net/manual/en/function.preg-match.php //http://www.regular-expressions.info/named.html preg_match("/(?Pabc)(.*)(?Pxyz)/", 'abcdefghijklmnopqrstuvwxyz', $matches); print_r($matches); ?> vince...@dev1:~$ php ./named_groups_test.php Array ( [0] => abcdefghijklmnopqrstuvwxyz [foo] => abc [1] => abc [2] => defghijklmnopqrstuvw [bar] => xyz [3] => xyz )