Which Characters Can Be Used to Enclose a Regular Expression Pattern in PHP?

Find out which characters you can use as delimiters to enclose regex pattern in PHP

In PHP, when you use regular expression patterns with PCRE (Perl Compatible Regular Expressions) functions (those that are prefixed with preg_* for example), you must enclose the pattern using delimiters. Most commonly used delimiters are forward slashes (/), hash signs (#) and tildes (~). However, you are not restricted to those only. In fact, you can use any non-alphanumeric, non-backslash, non-whitespace character. For example, all of the following are valid delimited patterns:

/foo bar/
#foo bar#
~foo bar~
|foo bar|
_foo bar_
+foo bar+
// ...

When to Escape the Delimiter Character?

You must remember that if the character you use for the delimiter (to enclose the pattern within) exists in the pattern itself, then you must escape it. For example:

// ...

To escape the delimiter character in the string when using preg_quote() function, you can use the second (optional) argument to the function to specify the delimiter. Otherwise, it will result in an error.

Using Brackets as Delimiters:

You can also use brackets (i.e. (), {}, [] and <>) as delimiters, where the opening and closing brackets are the starting and ending delimiter respectively:

(foo bar)
{foo bar}
[foo bar]
<foo bar>

When you're using bracket style delimiter pairs, you must remember the following:

  1. You do not need to escape the bracket delimiter characters when they're used as meta-characters within the pattern (i.e. when they're used as the special meaning they carry in regular expressions);
  2. You must escape them when the bracket delimiter itself appears in the string and the intention is to use it as a literal character.

To illustrate these, in the following example you can see how the brackets are not escaped, and thus they're interpreted as meta-characters:

$str = 'this i a pattern';
$replaceWith = 'foobar';

$pattern = '(this [is] a (pattern))';

echo preg_replace($pattern, $replaceWith, $str); // 'foobar'

In the example above, valid matches would be 'this i a pattern' and 'this s a pattern'.

If you wanted to literally match "this [is] a (pattern)", then you would need to escape the brackets:

$str = 'this [is] a (pattern)';
$replaceWith = 'foobar';

$pattern = '(this \[is\] a \(pattern\))';

echo preg_replace($pattern, $replaceWith, $str); // 'foobar'

Which Character Should You Use as a Delimiter?

You should always try to stick with the most commonly used delimiters (i.e. forward slashes, hash signs and tildes) to promote common understanding between different developers (especially when you're working in a team). However, if the delimiter appears often inside the pattern, it might be a good idea to choose another delimiter in order to increase readability.

Hope you found this post useful. It was published . Please show your love and support by sharing this post.