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.