I don't quite understand what you want to do and why you're doing it in such a complicated way, but \Q$needle\E escaping inside a regex will probably help you. See perlre for details.
In reply to Re: Is there a nicer way to do regex search and replace with literal backslashes?
by moritz
in thread Is there a nicer way to do regex search and replace with literal backslashes?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |