#46904 [Opn]: preg_match() example #4 is wrong

From: Date: Fri, 19 Dec 2008 10:05:35 +0000
Subject: #46904 [Opn]: preg_match() example #4 is wrong
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-1648@lists.php.net to get a copy of this message
ID: 46904 Updated by: rquadling@php.net Reported By: joe at digg dot com Status: Open Bug Type: Documentation problem Operating System: Debian GNU/Linux PHP Version: Irrelevant New Comment: I'm not so sure. Using RegexBuddy to explain the different Regexs ... There seems to be no difference between the 2 forms. (?<name>\w+): (?<digit>\d+) Options: case insensitive; ^ and $ match at line breaks Match the regular expression below and capture its match into backreference with name “name” «(?<name>\w+)» Match a single character that is a “word character” (letters, digits, etc.) «\w+» Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» Match the characters “: ” literally «: » Match the regular expression below and capture its match into backreference with name “digit” «(?<digit>\d+)» Match a single digit 0..9 «\d+» Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» (?P<name>\w+): (?P<digit>\d+) Options: case insensitive; ^ and $ match at line breaks Match the regular expression below and capture its match into backreference with name “name” «(?P<name>\w+)» Match a single character that is a “word character” (letters, digits, etc.) «\w+» Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» Match the characters “: ” literally «: » Match the regular expression below and capture its match into backreference with name “digit” «(?P<digit>\d+)» Match a single digit 0..9 «\d+» Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» Previous Comments: ------------------------------------------------------------------------ [2008-12-18 20:21:24] tobias382 at gmail dot com Patch for /phpdoc/en/reference/pcre/functions/preg-match.xml: 278c278 < preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches); --- > preg_match('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches); ------------------------------------------------------------------------ [2008-12-18 20:08:21] joe at digg dot com Description: ------------ On http://us.php.net/preg_match example #4 (Using named subpattern) is wrong. It shows: <?php $str = 'foobar: 2008'; preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches); print_r($matches); ?> The proper syntax for named expressions is (?P<foo>). Expected result: ---------------- <?php $str = 'foobar: 2008'; preg_match('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches); print_r($matches); ?> ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=46904&edit=1

« previous php.doc.bugs (#1648) next »