Documented the (?| syntax in PCRE.

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@301646 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Daniel Egeberg 2010-07-28 08:06:25 +00:00
parent 3109183fde
commit f49b14e3a0

View file

@ -1285,6 +1285,39 @@
also by name. PHP 5.2.2 introduced two alternative syntaxes
<literal>(?&lt;name&gt;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">