regex beginner

How to escape special characters in Perl regex?

Question

How to escape special characters in Perl regex?

In Perl, regular expressions (regex) have a set of special characters that control pattern matching, such as ., *, +, ?, ^, $, ( ), [ ], { }, |, and \\. These characters have special meanings and need to be escaped (i.e., preceded by a backslash) if you want to match them literally in a string.

Why escape special regex characters?

If you use a regex pattern with these characters unescaped, Perl will interpret them as regex operators rather than literal characters. This can lead to unintentionally matching multiple characters or an entirely different pattern than expected.

For example:

my $string = "Hello. How are you?";
if ($string =~ /Hello./) {
  print "Matched!\n";
}

The dot . here matches any character, so /Hello./ matches "Hello" followed by any character (like "Hello " or "Hello!"), not the literal string "Hello."

How to escape special characters

There are several ways to escape special characters in Perl regexes:

  • Manually add backslashes: For example, to match the literal dot, write /Hello\./.
  • Use the quotemeta function: This built-in function escapes all regex metacharacters in its argument by adding backslashes.
  • Use the \Q and \E escape sequences in regex: These tell Perl to treat everything between \Q and \E as literal text.

Example: Escaping regex special characters

Below is a runnable Perl script demonstrating each method:

use strict;
use warnings;

my $text = "Price is $10.99 (special offer!)";

# 1. Manually escaping special chars: escape $ and .
if ($text =~ /\$10\.99/) {
  print "Manual escape matched the price.\n";
}

# 2. quotemeta function
my $pattern = quotemeta('$10.99 (special offer!)');
if ($text =~ /$pattern/) {
  print "quotemeta matched the entire text.\n";
}

# 3. Using \Q...\E in regex pattern
if ($text =~ /\Q$10.99 (special offer!)\E/) {
  print "Using \\Q\\E matched the entire text.\n";
}

Explanation of techniques

  • quotemeta() escapes all regex metacharacters: .^$*+?()[{\\| by transforming them to \., \$, etc. This is useful when you want to treat arbitrary data literally inside a regex pattern.
  • \Q... and \E delimiters inside a regex pattern also escape all regex metas within, until \E is encountered. It’s convenient for escaping a variable directly inside a pattern.
  • Manual backslash escapes are error-prone, especially if the string comes from user input or external data, so prefer quotemeta or \Q...\E for robustness.

Common pitfalls

  • Remember that \\ is a literal backslash in Perl strings, so escaping a backslash in a regex pattern requires \\\\ inside double quotes (first for Perl, second for regex).
  • When inserting escaped strings inside regex delimiters, prefer qr// compiled regexes or quotemeta to avoid errors.
  • If you use single quotes around regex, backslashes behave differently—usually you want double quotes or qr// for regex expressions with variables.

Summary

To escape special regex characters in Perl, use either manual backslashes, quotemeta(), or the \Q...\E construct inside your regex pattern. For dynamic strings, prefer quotemeta() or \Q...\E. This ensures you match literal characters and avoid unintended pattern matching behavior.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 4ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
Matched!
STDERR
(empty)

Was this helpful?

Related Questions