Quote everything that could be a metacharacter before using the variable: s/(\W)/\\$1/g
There's also quotemeta, but I've been using the above pattern for years.
In reply to Re: Make a variable safe for a regex.
by kyle
in thread Make a variable safe for a regex.
by exodist
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |