Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 11023

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Subpatterns</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="regexp.reference.internal-options.html">Internal option setting</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="regexp.reference.repetition.html">Repetition</a></div>
 <div class="up"><a href="reference.pcre.pattern.syntax.html">PCRE regex syntax</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><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
     <em>cat(aract|erpillar|)</em> 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 class="emphasis">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
   
   <em>the ((red|white) (king|queen))</em>
   
   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
   
   <em>the ((?:red|white) (king|queen))</em>
   
   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 99, and the maximum number  of  all  subpatterns,
   both capturing and non-capturing, is 200.
  </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="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 
   <em>(?P&lt;name&gt;pattern)</em>. This subpattern will then 
   be indexed in the matches array by its normal numeric position and 
   also by name. PHP 5.2.2 introduced two alternative syntaxes 
   <em>(?&lt;name&gt;pattern)</em> and <em>(?&#039;name&#039;pattern)</em>.
  </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 <em>(?|</em> syntax allows
   having duplicate numbers. Consider the following regex matched against the
   string <em>Sunday</em>:
  </p>

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

  </div>

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

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

  </div>

  <p class="para">
   Using this pattern, both <em>Sun</em> and <em>Sat</em>
   would be stored in backreference 1.
  </p>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="regexp.reference.internal-options.html">Internal option setting</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="regexp.reference.repetition.html">Repetition</a></div>
 <div class="up"><a href="reference.pcre.pattern.syntax.html">PCRE regex syntax</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>