PHP and named groups in regex/PCRE

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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! :-)

vincentd@dev1:~$ cat ./named_groups_test.php 
<?php
    //http://us.php.net/manual/en/function.preg-match.php
    //http://www.regular-expressions.info/named.html

    preg_match("/(?P<foo>abc)(.*)(?P<bar>xyz)/",
               'abcdefghijklmnopqrstuvwxyz',
               $matches);
    print_r($matches);
?>

vincentd@dev1:~$ php ./named_groups_test.php 
Array
(
    [0] => abcdefghijklmnopqrstuvwxyz
    [foo] => abc
    [1] => abc
    [2] => defghijklmnopqrstuvw
    [bar] => xyz
    [3] => xyz
)


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux