Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am reasonably new to perl. I don't know how to frame a regex such that metacharacters in variables are treated literally. I would not know what these metacharacters will be before hand. Thanks. S
  • Comment on Metacharacters in a variable as literals in regex

Replies are listed 'Best First'.
Re: Metacharacters in a variable as literals in regex
by Abigail-II (Bishop) on Oct 09, 2003 at 11:52 UTC
    /\Q$variable\E/;

    See also the 'perlre' manual page.

    Abigail

Re: Metacharacters in a variable as literals in regex
by flounder99 (Friar) on Oct 09, 2003 at 12:23 UTC
    There is also quotemeta. Which does the same thing as \Q ... \E but outside the m//.
    $quotedregex = quotemeta $unquotedregex; m/$quotedregex/;

    --

    flounder