Perl quotemeta Function
last modified April 4, 2025
The Perl quotemeta
function escapes all special regex
metacharacters in a string. It prepends a backslash before each
metacharacter to make it literal.
quotemeta
is essential when working with regex patterns that
contain user input or dynamic content. It prevents regex metacharacters
from being interpreted as special pattern elements.
Basic quotemeta Usage
The simplest way to use quotemeta
is on a string containing
regex metacharacters.
#!/usr/bin/perl use strict; use warnings; use v5.34.0; my $text = 'Hello. (world) * $100'; my $escaped = quotemeta($text); print "Original: $text\n"; print "Escaped: $escaped\n";
We demonstrate quotemeta
escaping special characters in a
string. The function returns a new string with all metacharacters escaped.
$ ./basic.pl Original: Hello. (world) * $100 Escaped: Hello\.\ \(world\)\ \*\ \$100
Using quotemeta in Regex
quotemeta
is commonly used to safely include variables in regex.
#!/usr/bin/perl use strict; use warnings; use v5.34.0; my $search = 'file.txt'; my $text = 'Looking for file.txt in /path/to/file.txt'; if ($text =~ /\Q$search\E/) { print "Found exact match for '$search'\n"; } else { print "No match found\n"; }
This script uses \Q...\E
which internally uses quotemeta
to escape the variable. This ensures the dot is treated as literal.
$ ./regex.pl Found exact match for 'file.txt'
Comparing With and Without quotemeta
This example shows the difference between escaped and unescaped patterns.
#!/usr/bin/perl use strict; use warnings; use v5.34.0; my $pattern = 'file.*'; my $text = 'file.txt and file_data.csv'; print "Without quotemeta:\n"; if ($text =~ /$pattern/) { print "Match found (regex interpretation)\n"; } print "With quotemeta:\n"; if ($text =~ /\Q$pattern\E/) { print "Match found (literal interpretation)\n"; }
Without escaping, the dot and asterisk are treated as regex operators. With escaping, they're treated as literal characters.
$ ./compare.pl Without quotemeta: Match found (regex interpretation) With quotemeta: No match found
quotemeta on User Input
Always use quotemeta
when incorporating user input into regex.
#!/usr/bin/perl use strict; use warnings; use v5.34.0; print "Enter search pattern: "; my $input = <STDIN>; chomp $input; my $safe_pattern = quotemeta($input); my $text = 'Special chars: .*+?^$()[]{}|\/'; if ($text =~ /$safe_pattern/) { print "Found your pattern literally\n"; } else { print "Pattern not found\n"; }
This script safely handles user input that might contain regex metacharacters. The search will look for exact matches only.
quotemeta with File Paths
File paths often contain characters that are regex metacharacters.
#!/usr/bin/perl use strict; use warnings; use v5.34.0; my $path = '/usr/local/bin/perl'; my $escaped_path = quotemeta($path); print "Original path: $path\n"; print "Escaped path: $escaped_path\n"; if ('/usr/local/bin/perl' =~ /^$escaped_path$/) { print "Exact path match\n"; }
The forward slashes in the path are escaped, making them safe for regex matching. This ensures exact path comparison.
$ ./file_path.pl Original path: /usr/local/bin/perl Escaped path: \/usr\/local\/bin\/perl Exact path match
quotemeta in Substitutions
quotemeta
is useful when doing literal string replacements.
#!/usr/bin/perl use strict; use warnings; use v5.34.0; my $search = 'price: $100'; my $replace = 'cost: €85'; my $text = 'The price: $100 was reduced from price: $150'; my $safe_search = quotemeta($search); $text =~ s/$safe_search/$replace/g; print "Modified text: $text\n";
This script performs a literal substitution, treating all characters in the search pattern as literals, not regex metacharacters.
$ ./substitute.pl Modified text: The cost: €85 was reduced from price: $150
quotemeta with Special Characters
This example shows how quotemeta
handles various special chars.
#!/usr/bin/perl use strict; use warnings; use v5.34.0; my $special = '.*+?^$()[]{}|\/'; my $escaped = quotemeta($special); print "Original: $special\n"; print "Escaped: $escaped\n"; if ($special =~ /^$escaped$/) { print "Exact match after escaping\n"; }
The script escapes all regex metacharacters, allowing exact matching of strings containing these special characters.
$ ./special_chars.pl Original: .*+?^$()[]{}|\/ Escaped: \.\*\+\?\^\$\(\)\[\]\{\}\\\|\/ Exact match after escaping
Best Practices
- Always escape user input: Never trust user-provided patterns.
- Use \Q...\E for inline escaping: More readable than quotemeta().
- Combine with other regex: Mix literal and pattern matching.
- Document escaped patterns: Make code intentions clear.
Source
This tutorial covered Perl's quotemeta
function with practical
examples demonstrating its usage in common scenarios.
Author
List all Perl tutorials.