hide random home http://www.fmi.uni-passau.de/archive/doc/unix/perl/faq/2.12.html (Einblicke ins Internet, 10/1995)

How can I quote a variable to use in a regexp?

How can I quote a variable to use in a regexp?


    From the manual:

        $pattern =~ s/(\W)/\\$1/g;

    Now you can freely use /$pattern/ without fear of any unexpected
    meta-characters in it throwing off the search.  If you don't know
    whether a pattern is valid or not, enclose it in an eval to avoid
    a fatal run-time error.