Skip to content
PHP Logo

What is the significance of the dollar sign in the regular expression pattern?

The dollar sign in a regular expression pattern signifies the end of a string. This means that the pattern must match up to the end of the string for it to be considered a valid match. If the dollar sign is not included, the pattern could match anywhere within the string.

php
$pattern = "/hello$/"; // Match "hello" only at the end of the string
$string = "hello world";

if (preg_match($pattern, $string)) {
    echo "Match found!";
} else {
    echo "No match found.";
}