svn: /phpdoc/en/trunk/reference/pcre/ pattern.syntax.xml
| From: | Daniel Egeberg | Date: | Wed, 28 Jul 2010 08:06:25 +0000 |
| Subject: | svn: /phpdoc/en/trunk/reference/pcre/ pattern.syntax.xml | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-6933@lists.php.net to get a copy of this message | ||
degeberg Wed, 28 Jul 2010 08:06:25 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=301646
Log:
Documented the (?| syntax in PCRE.
Changed paths:
U phpdoc/en/trunk/reference/pcre/pattern.syntax.xml
Modified: phpdoc/en/trunk/reference/pcre/pattern.syntax.xml
===================================================================
--- phpdoc/en/trunk/reference/pcre/pattern.syntax.xml 2010-07-28 07:54:29 UTC (rev 301645)
+++ phpdoc/en/trunk/reference/pcre/pattern.syntax.xml 2010-07-28 08:06:25 UTC (rev 301646)
@@ -1285,6 +1285,39 @@
also by name. PHP 5.2.2 introduced two alternative syntaxes
<literal>(?<name>pattern)</literal> and
<literal>(?'name'pattern)</literal>.
</para>
+
+ <para>
+ 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 <literal>(?|</literal> syntax allows
+ having duplicate numbers. Consider the following regex matched against the
+ string <literal>Sunday</literal>:
+ </para>
+
+ <informalexample>
+ <programlisting>
+<![CDATA[(?:(Sat)ur|(Sun))day]]>
+ </programlisting>
+ </informalexample>
+
+ <para>
+ Here <literal>Sun</literal> is stored in backreference 2, while
+ backreference 1 is empty. Matching yields <literal>Sat</literal> in
+ backreference 1 while backreference 2 does not exist. Changing the pattern
+ to use the <literal>(?|</literal> fixes this problem:
+ </para>
+
+ <informalexample>
+ <programlisting>
+<![CDATA[(?|(Sat)ur|(Sun))day]]>
+ </programlisting>
+ </informalexample>
+
+ <para>
+ Using this pattern, both <literal>Sun</literal> and
<literal>Sat</literal>
+ would be stored in backreference 1.
+ </para>
</section>
<section xml:id="regexp.reference.repetition">