Regular Expressions Escaping /Delimiters/


Example

Many languages allow regex to be enclosed or delimited between a couple of specific characters, usually the forward slash /.

Delimiters have an impact on escaping: if the delimiter is / and the regex needs to look for / literals, then the forward slash must be escaped before it can be a literal (\/).

Excessive escaping harms readability, so it's important to consider the available options:

Javascript is unique because it allows forward slash as a delimiter, but nothing else (although it does allow stringified regexes).

Perl1

Perl, for example, allows almost anything to be a delimiter. Even Arabic characters:

$str =~ m ش ش

Specific rules are mentioned in Perl's documentation.

PCRE allows two types of delimiters: matched delimiters and bracket-style delimiters. Matched delimiters make use of a single character's pair, while bracket-style delimiters make use of a couple of characters which represents an opening and closing pair.

  • Matching delimiters: !"#$%&'*+,./:;=?@^_`|~-
  • Bracket-style delimiters: (), {}, [], <>