<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/reference.pcre.pattern.syntax.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'de',
  ),
  'this' => 
  array (
    0 => 'regexp.reference.subpatterns.php',
    1 => 'Subpatterns',
    2 => 'Subpatterns',
  ),
  'up' => 
  array (
    0 => 'reference.pcre.pattern.syntax.php',
    1 => 'PCRE regex syntax',
  ),
  'prev' => 
  array (
    0 => 'regexp.reference.internal-options.php',
    1 => 'Internal option setting',
  ),
  'next' => 
  array (
    0 => 'regexp.reference.repetition.php',
    1 => 'Repetition',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/pcre/pattern.syntax.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="regexp.reference.subpatterns" class="section">
  <h2 class="title">Subpatterns</h2>
  <p class="para">
   Subpatterns are delimited by parentheses (round brackets),
   which can be nested. Marking part of a pattern as a subpattern
   does two things:
  </p>
  <ol type="1">
   <li class="listitem">
    <p class="para">
     It localizes a set of alternatives. For example, the pattern
     <code class="literal">cat(aract|erpillar|)</code> matches one of the words &quot;cat&quot;,
     &quot;cataract&quot;, or &quot;caterpillar&quot;. Without the parentheses, it would match
     &quot;cataract&quot;, &quot;erpillar&quot; or the empty string.
    </p>
   </li>
   <li class="listitem">
    <p class="para">
     It sets up the subpattern as a capturing subpattern (as defined above).
     When the whole pattern matches, that portion of the subject string
     that matched the subpattern is passed back to the caller via the
     <em>ovector</em> argument of <span class="function"><strong>pcre_exec()</strong></span>.
     Opening parentheses are counted from left to right (starting from 1) to
     obtain the numbers of the capturing subpatterns.
    </p>
   </li>
  </ol>
  <p class="para">
   For example, if the string &quot;the red king&quot; is matched against
   the pattern

   <code class="literal">the ((red|white) (king|queen))</code>

   the captured substrings are &quot;red king&quot;, &quot;red&quot;, and &quot;king&quot;,
   and are numbered 1, 2, and 3.
  </p>
  <p class="para">
   The fact that plain parentheses fulfill two functions is not
   always helpful. There are often times when a grouping subpattern
   is required without a capturing requirement. If an
   opening parenthesis is followed by &quot;?:&quot;, the subpattern does
   not do any capturing, and is not counted when computing the
   number of any subsequent capturing subpatterns. For example,
   if the string &quot;the white queen&quot; is matched against the
   pattern

   <code class="literal">the ((?:red|white) (king|queen))</code>

   the captured substrings are &quot;white queen&quot; and &quot;queen&quot;, and
   are numbered 1 and 2. The maximum number of captured substrings
   is 65535. It may not be possible to compile such large patterns,
   however, depending on the configuration options of libpcre.
  </p>
  <p class="para">
   As a convenient shorthand, if any option settings are
   required at the start of a non-capturing subpattern, the
   option letters may appear between the &quot;?&quot; and the &quot;:&quot;. Thus
   the two patterns
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="annotation-interactive cdata"><pre>
(?i:saturday|sunday)
(?:(?i)saturday|sunday)
</pre></div>
   </div>

  </div>

  <p class="para">
   match exactly the same set of strings. Because alternative
   branches are tried from left to right, and options are not
   reset until the end of the subpattern is reached, an option
   setting in one branch does affect subsequent branches, so
   the above patterns match &quot;SUNDAY&quot; as well as &quot;Saturday&quot;.
  </p>

  <p class="para">
   It is possible to name a subpattern using the syntax
   <code class="literal">(?P&lt;name&gt;pattern)</code>. This subpattern will then
   be indexed in the matches array by its normal numeric position and
   also by name. There are two alternative syntaxes
   <code class="literal">(?&lt;name&gt;pattern)</code> and <code class="literal">(?&#039;name&#039;pattern)</code>.
  </p>

  <p class="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 <code class="literal">(?|</code> syntax allows
   having duplicate numbers. Consider the following regex matched against the
   string <code class="literal">Sunday</code>:
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="annotation-interactive cdata"><pre>(?:(Sat)ur|(Sun))day</pre></div>
   </div>

  </div>

  <p class="para">
   Here <code class="literal">Sun</code> is stored in backreference 2, while
   backreference 1 is empty. Matching <code class="literal">Saturday</code> yields
   <code class="literal">Sat</code> in backreference 1 while backreference 2 does
   not exist. Changing the pattern to use the <code class="literal">(?|</code> fixes
   this problem:
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="annotation-interactive cdata"><pre>(?|(Sat)ur|(Sun))day</pre></div>
   </div>

  </div>

  <p class="para">
   Using this pattern, both <code class="literal">Sun</code> and <code class="literal">Sat</code>
   would be stored in backreference 1.
  </p>
 </div><?php manual_footer($setup); ?>