Chapter 3: Anchors & Boundaries
Goal: Match positions in a string, not characters.
1) Positions, not characters
Most of what we’ve seen so far matches characters.
Anchors are different. They don’t consume any text, they just say: “the match is only allowed at this position”.
Think of them like bookmarks: start of string, end of string, or the edge between a word and something else.
2) Start and End: ^ and $
^ — start of the string
^ matches the very beginning of the subject — the string the pattern is run against (preg_match()’s second argument).
echo preg_match('/^cat/', 'cat food'), PHP_EOL; // 1
echo preg_match('/^cat/', 'The cat'), PHP_EOL; // 0 (not at start)
$ — end of the string
$ matches the very end of the subject.
echo preg_match('/cat$/', 'The cat'), PHP_EOL; // 1
echo preg_match('/cat$/', 'cat food'), PHP_EOL; // 0 (not at end)
Matching the entire string
Combine ^ and $ to require an exact match of the whole string.
echo preg_match('/^cat$/', 'cat'), PHP_EOL; // 1
echo preg_match('/^cat$/', 'cat food'), PHP_EOL; // 0
echo preg_match('/^cat$/', 'The cat'), PHP_EOL; // 0
This is extremely useful for validation. If you want “this input must be exactly X”, anchors are your friend.
3) A Quick Word About Multiline
With the m (multiline) flag, ^ and $ can also match at the start/end of each line instead of only the whole string.
$text = "cat\nbird";
echo preg_match('/^cat/m', $text), PHP_EOL; // 1 (start of first line)
echo preg_match('/^bird/m', $text), PHP_EOL; // 1 (start of second line)
For now, just know this exists. We’ll not rely on m in this chapter’s exercises.
4) Absolute Anchors: \A and \z
Sometimes you want to be strict and ignore the multiline behavior entirely.
\A— matches only the absolute start of the string\z— matches only the absolute end of the string
$text = "cat\nbird";
echo preg_match('/\Acat/m', $text), PHP_EOL; // 1 (absolute start)
echo preg_match('/\Abird/m', $text), PHP_EOL; // 0
echo preg_match('/bird\z/m', $text), PHP_EOL; // 1 (absolute end)
The trailing newline trap
One subtle point: without m, $ doesn’t only match at the very end — it also accepts the position right before a newline that is the last character of the subject. That’s why /^cat$/ matches "cat\n": the $ sits just before that final newline. \z never does this — it only matches at the very end, with nothing after it, not even a newline.
echo preg_match('/^cat$/', "cat\n"), PHP_EOL; // 1 (before the final newline)
echo preg_match('/^cat\z/', "cat\n"), PHP_EOL; // 0
If you truly mean “the entire input must be exactly this”, \A...\z is the clearest and safest choice.
5) Word Boundaries \b
A word boundary \b matches a position between a word character (\w: letters, digits, underscore) and a non-word character (\W), or between a word character and the start/end of the string.
Crucially, \b matches a position, not a space. It won’t add extra characters to your match.
Matching whole words
echo preg_match('/\bcat\b/', 'the cat sat'), PHP_EOL; // 1
echo preg_match('/\bcat\b/', 'category'), PHP_EOL; // 0 (cat is part of the word)
echo preg_match('/\bcat\b/', 'wildcat'), PHP_EOL; // 0
Seeing it differently
| Pattern | Text | Match | Explanation |
|---|---|---|---|
/\bcat/ |
"category" |
yes | Boundary before cat, but no boundary after it |
/cat\b/ |
"wildcat" |
yes | Boundary after cat, but no boundary before it |
/\bcat\b/ |
"the cat" |
yes | Boundaries on both sides |
You can also use \B for the opposite: a position where there is no word boundary (i.e. between two word characters, or two non-word characters).
echo preg_match('/\Bcat\B/', 'concatenate'), PHP_EOL; // 1 ("cat" in the middle)
echo preg_match('/\Bcat\B/', 'the cat'), PHP_EOL; // 0
You won’t need \B often as a beginner, but it’s good to know it exists.
6) Putting It Together
Anchors and boundaries become really practical when you mix them with character classes and quantifiers.
A few examples to get the idea:
// Only digits
echo preg_match('/^\d+$/', '12345'), PHP_EOL; // 1
// Starts with "http://"
echo preg_match('/^https?:\/\//', 'https://example.com'), PHP_EOL; // 1
// Ends with ".txt"
echo preg_match('/\.txt$/', 'readme.txt'), PHP_EOL; // 1
// Whole word only
echo preg_match('/\berror\b/', 'error: failed'), PHP_EOL; // 1
echo preg_match('/\berror\b/', 'errorCode'), PHP_EOL; // 0
Don’t worry about + (in \d+) or the ? in https? yet — ? makes the s optional. Both are quantifiers, covered in the next chapter.
7) Exercises
Only use what you’ve learned in this chapter and the previous ones.
Exercise 1: Start of string
Write a pattern that matches strings that start with hello.
Solution
'/^hello/'
Exercise 2: End of string
Write a pattern that matches strings that end with .txt.
Solution
'/\.txt$/'
Exercise 3: Exact match
Write a pattern that matches only the exact string PHP.
Solution
'/^PHP$/'
Exercise 4: Whole word
Write a pattern that matches the word test only when it’s a separate word.
Solution
'/\btest\b/'
Exercise 5: Strict from start to end
Write a pattern using \A and \z that matches exactly cat.
Solution
'/\Acat\z/'
Key Takeaways
- Anchors (
^,$) match positions, not characters. ^= start of string,$= end of string.- Use
^...$to match the entire string. \Aand\zare always absolute (ignore multiline).$can match before a final newline.\zdoes not.- Word boundaries
\bmatch the edge between\wand\W. \Bmatches where there is no word boundary.
Common Pitfalls
- Forgetting anchors in validation —
/\d+/will match digits inside a longer string./^\d+$/requires the whole string to be digits. - Mixing up
\band spaces —\bdoesn’t match a space, it matches the position between space and word. - Using
$when you mean “end, no newline” — in strict validation, consider\zover$.
Optional: Tools to Inspect Patterns
If you’d like to see how anchors and boundaries are parsed, these optional tools can help:
vendor/bin/regex explain '/^\d+$/'
vendor/bin/regex diagram '/\bcat\b/'
use PHPRegex\Toolkit\Regex;
$regex = Regex::create();
echo $regex->explain('/^hello$/');