Please wait

Anchors

Anchors in regular expressions are like signposts that help you match specific parts of a text.

Imagine you're looking for a word at the very beginning of a sentence or at the very end of a sentence. Anchors help you do just that!

  • The caret symbol (^) is like a signpost for the beginning of a line. If you use it before a word or pattern, it'll help you find matches only at the start of a line.
  • The dollar symbol ($) is a signpost for the end of a line. If you use it after a word or pattern, it'll help you find matches only at the end of a line.

So, anchors help you pinpoint exactly where you want to look in the text, either at the beginning or the end of a line.

Matching the Beginning

The caret symbol (^) in a regular expression for PHP is used to match the beginning of a line. It ensures that the pattern you're looking for is found only if it appears at the start of a line or string.

Suppose you have the following text and you want to check if the text starts with "Hello":

$text = "Hello, World!";
$pattern = '/^Hello/';
 
if (preg_match($pattern, $text)) {
  echo "The text starts with 'Hello'";
} else {
  echo "The text does not start with 'Hello'";
}

So, the caret symbol (^) acts like a guide, telling the regular expression to only find "Hello" if it's at the very start of a line. It's like saying, "Only look for 'Hello' if it's the first thing on the line."

Matching the End

The dollar symbol ($) in a regular expression for PHP is used to match the end of a line or string. It ensures that the pattern you're looking for is found only if it appears at the very end of a line.

Imagine you want to find out if a given text ends with the word "World" You can use the dollar symbol ($) in your pattern like this: World$.

$text = "Hello, World";
$pattern = '/World$/';
 
if (preg_match($pattern, $text)) {
  echo "The text ends with 'World'";
} else {
  echo "The text does not end with 'World'";
}

In this example, the regular expression '/World$/' will look for the string "World" only at the end of the $text string.

So, the dollar symbol ($) acts as a guide, telling the regular expression to only find the specified pattern if it's at the very end of a line or string. It's like saying, "Only look for this pattern if it's the last thing on the line."

Full Matches

The caret symbol (^) and dollar symbol ($) can be used together in a pattern like ^...$ to make sure that a string exactly matches a specific format. This can be handy when you want to verify that user input is in the correct form.

Suppose you need to check if a string represents a time in the "12:34" format. In other words, the string must have two digits, a colon, and then two more digits.

You can write a regular expression like ^\d\d:\d\d$ to represent this pattern:

$goodInput = "12:34";
$badInput = "12:345";
$pattern = '/^\d\d:\d\d$/';
 
if (preg_match($pattern, $goodInput)) {
  echo "Good input is in the right format!"; // This will be printed
} else {
  echo "Good input is not in the right format!";
}
 
if (preg_match($pattern, $badInput)) {
  echo "Bad input is in the right format!";
} else {
  echo "Bad input is not in the right format!"; // This will be printed
}

The pattern '/^\d\d:\d\d$/' checks that the matching part must start immediately at the beginning of the text (^) and end right at the end of the text ($).

The entire string must follow this exact format. If there's even a single extra character or any other deviation from the pattern, the result will be false, indicating that the string does not match the expected format.

Anchors are zero-width

The caret symbol (^) and the dollar symbol ($) in regular expressions are unique because they don't actually match any specific characters. So, rather than matching characters, they ensure that the pattern matches exactly at the start or end of the entire text.

Exercise

Which string matches the pattern ^$?

Key Takeaways

  • Anchors are not characters. They don't match characters themselves but rather check specific conditions in the text (like the start or end of a string).
  • The ^ anchor checks if the pattern appears at the very beginning of the text. If it does, the match is successful.
  • The $ anchor checks if the pattern appears at the very end of the text. If it does, the match is successful.
  • When used together, they ensure that the entire string exactly matches the pattern. If there's any deviation, the result is false.
  • Anchors are considered to have "zero width." They don't consume any characters in the text; they just assert a position.

Comments

Please read this before commenting