Regex Anchors
Summary: in this tutorial, you’ll learn how to use the regex anchors to match the start and the end of a string.
Introduction to the regex anchors
Unlike character classes, the regex anchors do not match characters, but a position before or after characters:
^
: The caret anchor (^
) matches the beginning of a string.$
: the dollar anchor ($
) matches the end of a string.
The following example uses a regular expression that matches any character P
in a string:
$pattern = '/P/';
$str = 'PHP';
if (preg_match_all($pattern, $str, $matches)) {
print_r($matches[0]);
}
Code language: PHP (php)
It returns two characters P
in the string PHP
:
Array
(
[0] => P
[1] => P
)
Code language: PHP (php)
However, if you add the caret anchor (^
) to the regular expression, it’ll match only the letter P
at the beginning of the string:
$pattern = '/^P/';
$str = 'PHP';
if (preg_match_all($pattern, $str, $matches)) {
print_r($matches[0]);
}
Code language: PHP (php)
Output:
Array
(
[0] => P
)
Code language: PHP (php)
The following example uses the dollar anchor ($
) to match a number at the end of a string:
$pattern = '/\d$/';
$str = 'PHP 8';
if (preg_match_all($pattern, $str, $matches)) {
print_r($matches[0]);
}
Code language: PHP (php)
It returns the number 8 as expected:
Array
(
[0] => 8
)
Code language: PHP (php)
The following regular expression uses the caret (^
) and dollar ($
) anchors to match a time string with the format hh:mm
:
$pattern = '/^\d\d:\d\d$/';
$message = '12:15';
if (preg_match_all($pattern, $message, $matches)) {
print_r($matches[0]);
}
Code language: PHP (php)
Output:
Array
(
[0] => 12:15
)
Code language: PHP (php)
Regex anchor and multiline mode
By default, the caret ^
and $
anchors treat a string as a single line of character even if the string contains newline characters. For example:
$pattern = '/^\d/';
$message = <<<'text'
1. First item
2. Second item
3. Third item
text;
if (preg_match_all($pattern, $message, $matches)) {
print_r($matches[0]);
}
Code language: PHP (php)
Output:
Array
(
[0] => 1
)
Code language: PHP (php)
In this example, the regular expression "/^\d/"
matches a digit at the beginning of a string. It returns the number 1 as expected.
In some cases, you may want to match a digit at the beginning of the line instead, not just the beginning of the string.
To do that, you add the m
flag at the end of the regular expression like this:
$pattern = '/^\d/m';
$message = <<<'text'
1. First item
2. Second item
3. Third item
text;
if (preg_match_all($pattern, $message, $matches)) {
print_r($matches[0]);
}
Code language: PHP (php)
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Code language: PHP (php)
The m' flag stands for multiline. When this
m’ flag is set, the caret (^
) and dollar ($
) anchor will match the start and the end of each line in the string.
Note that if a string doesn’t have any newlines, the m
flag will have no effect.
Summary
- The caret anchor (
^
) matches at the start of a string. - The dollar anchor (
$
) matches at the end of a string. - The
m
flag instructs those anchors to match at the beginning and end of the line.