From f49b14e3a0ef690a805a1605367ec0f87c67884a Mon Sep 17 00:00:00 2001 From: Daniel Egeberg Date: Wed, 28 Jul 2010 08:06:25 +0000 Subject: [PATCH] Documented the (?| syntax in PCRE. git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@301646 c90b9560-bf6c-de11-be94-00142212c4b1 --- reference/pcre/pattern.syntax.xml | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/reference/pcre/pattern.syntax.xml b/reference/pcre/pattern.syntax.xml index f5d093d77c..c7cd6a4b0e 100644 --- a/reference/pcre/pattern.syntax.xml +++ b/reference/pcre/pattern.syntax.xml @@ -1285,6 +1285,39 @@ also by name. PHP 5.2.2 introduced two alternative syntaxes (?<name>pattern) and (?'name'pattern). + + + Sometimes it is necessary to have multiple matching, but alternating + subgroups in a regular expression. Normally, each of these would be given + their own backreference number even though only one of them would ever + possibly match. To overcome this, the (?| syntax allows + having duplicate numbers. Consider the following regex matched against the + string Sunday: + + + + + + + + + + Here Sun is stored in backreference 2, while + backreference 1 is empty. Matching yields Sat in + backreference 1 while backreference 2 does not exist. Changing the pattern + to use the (?| fixes this problem: + + + + + + + + + + Using this pattern, both Sun and Sat + would be stored in backreference 1. +