Source of: /manual/en/regexp.reference.circudollar.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.circudollar.php',
1 => 'Circumflex and dollar',
),
'up' =>
array (
0 => 'reference.pcre.pattern.syntax.php',
1 => 'Pattern Syntax',
),
'prev' =>
array (
0 => 'regexp.reference.unicode.php',
1 => 'Unicode character properties',
),
'next' =>
array (
0 => 'regexp.reference.dot.php',
1 => 'Full stop',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="regexp.reference.circudollar" class="section">
<h2 class="title">Circumflex and dollar</h2>
<p class="para">
Outside a character class, in the default matching mode, the
circumflex character (<i>^</i>) is an assertion which
is true only if the current matching point is at the start of
the subject string. Inside a character class, circumflex (<i>^</i>)
has an entirely different meaning (see below).
</p>
<p class="para">
Circumflex (<i>^</i>) need not be the first character
of the pattern if a number of alternatives are involved, but it
should be the first thing in each alternative in which it appears
if the pattern is ever to match that branch. If all possible
alternatives start with a circumflex (<i>^</i>), that is,
if the pattern is constrained to match only at the start of the subject,
it is said to be an "anchored" pattern. (There are also other
constructs that can cause a pattern to be anchored.)
</p>
<p class="para">
A dollar character (<i>$</i>) is an assertion which is
<b><tt class="constant">TRUE</tt></b> only if the current matching point is at the end of the subject
string, or immediately before a newline character that is the last
character in the string (by default). Dollar (<i>$</i>)
need not be the last character of the pattern if a number of
alternatives are involved, but it should be the last item in any branch
in which it appears. Dollar has no special meaning in a
character class.
</p>
<p class="para">
The meaning of dollar can be changed so that it matches only
at the very end of the string, by setting the
<a href="reference.pcre.pattern.modifiers.php" class="link">PCRE_DOLLAR_ENDONLY</a>
option at compile or matching time. This does not affect the \Z assertion.
</p>
<p class="para">
The meanings of the circumflex and dollar characters are
changed if the
<a href="reference.pcre.pattern.modifiers.php" class="link">PCRE_MULTILINE</a> option
is set. When this is the case, they match immediately after and
immediately before an internal "\n" character, respectively, in addition
to matching at the start and end of the subject string. For example, the
pattern /^abc$/ matches the subject string "def\nabc" in multiline mode,
but not otherwise. Consequently, patterns that are anchored in single
line mode because all branches start with "^" are not anchored in
multiline mode. The
<a href="reference.pcre.pattern.modifiers.php" class="link">PCRE_DOLLAR_ENDONLY</a>
option is ignored if
<a href="reference.pcre.pattern.modifiers.php" class="link">PCRE_MULTILINE</a> is
set.
</p>
<p class="para">
Note that the sequences \A, \Z, and \z can be used to match
the start and end of the subject in both modes, and if all
branches of a pattern start with \A is it always anchored,
whether <a href="reference.pcre.pattern.modifiers.php" class="link">PCRE_MULTILINE</a>
is set or not.
</p>
</div><?php manual_footer(); ?>