downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

Source of: /manual/en/regexp.reference.assertions.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = 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 => 'en',
  ),
 
'this' =>
  array (
   
0 => 'regexp.reference.assertions.php',
   
1 => 'Assertions',
  ),
 
'up' =>
  array (
   
0 => 'reference.pcre.pattern.syntax.php',
   
1 => 'Pattern Syntax',
  ),
 
'prev' =>
  array (
   
0 => 'regexp.reference.back-references.php',
   
1 => 'Back references',
  ),
 
'next' =>
  array (
   
0 => 'regexp.reference.onlyonce.php',
   
1 => 'Once-only subpatterns',
  ),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);

manual_header();
?>
<div id="regexp.reference.assertions" class="section">
     <h2 class="title">Assertions</h2>
     <p class="para">
     An assertion is  a  test  on  the  characters  following  or
     preceding  the current matching point that does not actually
     consume any characters. The simple assertions coded  as  \b,
     \B,  \A,  \Z,  \z, ^ and $ are described above. More complicated
     assertions are coded as  subpatterns.  There  are  two
     kinds:  those that <em class="emphasis">look ahead</em> of the current position in the
     subject string, and those that <em class="emphasis">look behind</em> it.
    </p>
    <p class="para">
     An assertion subpattern is matched in the normal way, except
     that  it  does not cause the current matching position to be
     changed. <em class="emphasis">Lookahead</em> assertions start with  (?=  for  positive
     assertions and (?! for negative assertions. For example,

       <i>\w+(?=;)</i>

     matches a word followed by a semicolon, but does not include
     the semicolon in the match, and

       <i>foo(?!bar)</i>

     matches any occurrence of &quot;foo&quot;  that  is  not  followed  by
     &quot;bar&quot;. Note that the apparently similar pattern

       <i>(?!foo)bar</i>

     does not find an occurrence of &quot;bar&quot;  that  is  preceded  by
     something other than &quot;foo&quot;; it finds any occurrence of &quot;bar&quot;
     whatsoever, because the assertion  (?!foo)  is  always  <b><tt class="constant">TRUE</tt></b>
     when  the  next  three  characters  are  &quot;bar&quot;. A lookbehind
     assertion is needed to achieve this effect.
    </p>
    <p class="para">
     <em class="emphasis">Lookbehind</em> assertions start with (?&lt;=  for  positive  assertions
     and (?&lt;! for negative assertions. For example,

       <i>(?&lt;!foo)bar</i>

     does find an occurrence of &quot;bar&quot; that  is  not  preceded  by
     &quot;foo&quot;. The contents of a lookbehind assertion are restricted
     such that all the strings  it  matches  must  have  a  fixed
     length.  However, if there are several alternatives, they do
     not all have to have the same fixed length. Thus

       <i>(?&lt;=bullock|donkey)</i>

     is permitted, but

       <i>(?&lt;!dogs?|cats?)</i>

     causes an error at compile time. Branches  that  match  different
     length strings are permitted only at the top level of
     a lookbehind assertion. This is an extension  compared  with
     Perl  5.005,  which  requires all branches to match the same
     length of string. An assertion such as

       <i>(?&lt;=ab(c|de))</i>

     is not permitted, because its single  top-level  branch  can
     match two different lengths, but it is acceptable if rewritten
     to use two top-level branches:

       <i>(?&lt;=abc|abde)</i>

     The implementation of lookbehind  assertions  is,  for  each
     alternative,  to  temporarily move the current position back
     by the fixed width and then  try  to  match.  If  there  are
     insufficient  characters  before  the  current position, the
     match is deemed to fail.  Lookbehinds  in  conjunction  with
     once-only  subpatterns can be particularly useful for matching
     at the ends of strings; an example is given at  the  end
     of the section on once-only subpatterns.
    </p>
    <p class="para">
     Several assertions (of any sort) may  occur  in  succession.
     For example,

       <i>(?&lt;=\d{3})(?&lt;!999)foo</i>

     matches &quot;foo&quot; preceded by three digits that are  not  &quot;999&quot;.
     Notice  that each of the assertions is applied independently
     at the same point in the subject string. First  there  is  a
     check  that  the  previous  three characters are all digits,
     then there is a check that the same three characters are not
     &quot;999&quot;.   This  pattern  does not match &quot;foo&quot; preceded by six
     characters, the first of which are digits and the last three
     of  which  are  not  &quot;999&quot;.  For  example,  it doesn&#039;t match
     &quot;123abcfoo&quot;. A pattern to do that is

       <i>(?&lt;=\d{3}...)(?&lt;!999)foo</i>
    </p>
    <p class="para">
     This time the first assertion looks  at  the  preceding  six
     characters,  checking  that  the first three are digits, and
     then the second assertion checks that  the  preceding  three
     characters are not &quot;999&quot;.
    </p>
    <p class="para">
     Assertions can be nested in any combination. For example,

       <i>(?&lt;=(?&lt;!foo)bar)baz</i>

     matches an occurrence of &quot;baz&quot; that  is  preceded  by  &quot;bar&quot;
     which in turn is not preceded by &quot;foo&quot;, while

       <i>(?&lt;=\d{3}...(?&lt;!999))foo</i>

     is another pattern which matches  &quot;foo&quot;  preceded  by  three
     digits and any three characters that are not &quot;999&quot;.
    </p>
    <p class="para">
     Assertion subpatterns are not capturing subpatterns, and may
     not  be  repeated,  because  it makes no sense to assert the
     same thing several times. If any kind of assertion  contains
     capturing  subpatterns  within it, these are counted for the
     purposes of numbering the capturing subpatterns in the whole
     pattern.   However,  substring capturing is carried out only
     for positive assertions, because it does not make sense  for
     negative assertions.
    </p>
    <p class="para">
     Assertions count towards the maximum  of  200  parenthesized
     subpatterns.
     </p>
    </div><?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites