正则中的Named Subpattern 版本差别

Example #4 Using named subpattern php

<?php

$str 
'foobar: 2008' ;

preg_match ( '/(?<name>/w+): (?<digit>/d+)/' $str $matches );

print_r ( $matches );

?>

The above example will output:git

Array
(
[0] => foobar: 2008
[name] => foobar
[1] => foobar
[digit] => 2008
[2] => 2008

以上例子在php5.16是有问题的,将输出一个空的数组,意思是匹配不到。如要这样才行:

<?php

$str 
'foobar: 2008' ;

preg_match ( '/(?<name>/w+): (?P<digit>/d+)/' $str $matches );

print_r ( $matches );

?>
差别就在于(?<与(?P< 。