- Perl ›
- builtin functions ›
- here
quotemeta function - regular expression character escaping
You can use the quotemeta function to treat strings used in regular expression as normal characters. All characters except word characters are preceded by \.
# Treat regular expression strings as normal characters $ret = quotemeta $str;
This is an example that pattern matches a string containing regular expression characters as a normal string.
# Pattern matching using a string containing regular expression characters as a normal string my $message = 'This message contains a{2}'; my $match = "a{2}"; $match = quotemeta $match; if ($message =~ /$match/) { print "Match!\n"; }
quotemeta has the same meaning as the regular expression special character\Q. The above quotemeta does not have the same meaning as the following description.
Express the same meaning as quotemeta using #\Q my $match = "\Qa{2}";
For more information on a regular expression, see Practical Perl Regular Expressions Complete Explanation.