regex - What is the perl equivalent of PHP's preg_quote function? -
i have string want match within perl regex pattern, characters in may need escaping. in php, function preg_quote. equivalent in perl?
if want match contents of variable literally, enclose in \q...\e
quotes, or use quotemeta
function. both of these work:
use feature 'say'; $metachars = "1+2"; $escaped_metachars = quotemeta $metachars; local $_ = "1112, 1+2"; "\\q...\\e: ", /(\q$metachars\e)/; "quotemeta: ", /($escaped_metachars)/; "no escaping: ", /($metachars)/;
output:
\q...\e: 1+2 quotemeta: 1+2 no escaping: 1112
Comments
Post a Comment