PHP regular expressions: No ending delimiter '^' found in

PHP regular expressions: No ending delimiter '^' found in

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP Regular Expressions: No Ending Delimiter Found?

You are staring at a warning message from preg_match(): "No ending delimiter '^' found in". The panic is understandable because the error explicitly mentions the caret character ^, which is part of your pattern string. However, the root cause lies not with the regex syntax itself, but with how PHP functions expect to receive that syntax.

In PHP, regular expressions are not strings; they are objects created by specific wrapper functions like preg_match(), preg_replace(), or preg_split(). These wrappers follow a strict signature: function_name(delimiter, pattern, subject). The first argument is the delimiter (commonly / or #), and the second argument is the actual pattern string. Crucially, the pattern string must be enclosed in quotes to be treated as a string literal after the delimiter is parsed by PHP.

Consider the code snippet causing the error:

$pattern = "^([0-9]+)$";

if (preg_match($pattern, $input)) {
    echo "yes";
} else {
    echo "nope";
}

The logic here appears sound at first glance. You have defined a pattern string starting with ^ and ending with $. When you pass this to preg_match(), the function attempts to parse it as if it were passed directly, expecting the structure /pattern/. Because $pattern is just a string variable without the surrounding delimiters, PHP tries to interpret the first character of that string as the delimiter. It sees ^ and thinks, "Okay, this is my delimiter," but then looks for a closing ^ to match it. It never finds one because the actual pattern content follows immediately. This mismatch triggers the specific warning you are seeing.

To resolve this, you must explicitly wrap the string variable in delimiters when passing it to the function. The most common approach is using forward slashes:

$pattern = "^([0-9]+)$";

if (preg_match("/$pattern/", $input)) {
    echo "yes";
} else {
    echo "nope";
}

By adding / at the start and end, you tell PHP: "The delimiter is /, and the pattern inside is $pattern." Now, the function correctly interprets ^ as the start-of-string anchor and $ as the end-of-string anchor within the regex engine.

Alternatively, if you prefer to define the entire regex string with delimiters already included, you can assign it directly:

$pattern = '/^([0-9]+)$/';

if (preg_match($pattern, $input)) {
    echo "yes";
} else {
    echo "nope";
}

This approach is often cleaner because the delimiters are part of the string literal itself, eliminating the need to reconstruct them during function calls. It also prevents issues if your pattern legitimately contains a forward slash, provided you choose a different delimiter like # or ~.

A subtle but frequent pitfall occurs when building patterns dynamically by concatenating strings. If you construct $pattern using string concatenation and accidentally forget to quote the final part, or if the variable contains characters that confuse the parser, similar errors arise. Always ensure the final argument passed to preg_match() is a complete, quoted string (either a literal or a properly delimited variable).

In modern PHP development, particularly when working with frameworks like Laravel, this syntax nuance can trip up developers who are used to JavaScript regex literals or Python raw strings where delimiters are implicit. Remember that PHP's PCRE functions require explicit delimiters unless you use the newer preg_match() variant that accepts a string directly in some specific contexts (though the standard wrapper still expects them).

For complex applications, consider using named capturing groups if you need to extract data, but keep the delimiter logic consistent throughout your codebase. If you are migrating from an older PHP version or working with legacy codebases where this error appears frequently, auditing all preg_* calls for missing delimiters is a high-priority task.

Our network