if the point is to quote a string for use within a regexp so that metachars within the string are interpreted literally, you probably want to use \Q and \E, which will automatically take care of everything that is treated special in regexps
However, it also looks like you're trying to simultaneously
... which is going to get massively confusing if, as usually happens in quoted string formats, there are characters that will be starting out backslash-escaped (e.g., in most (but not all! cf. DOS command lines or Visual Basic...) double-quoted string formats, the double-quote (") character itself will be escaped, in which case you don't want to be escaping it again).
Best way to preserve your sanity would be to treat the unescaping from double-quoted format and re-escaping for use within a regexp as two separate operations. I.e., strip the outer quotes and unescape everything inside that needs to be unescaped — this gets you to the actual raw string — then worry about getting it into whatever regexp you're using to match with. (Yes, this is slightly less efficient; but get it right first then worry about optimizing...).
For the unescaping operation you want something like
(the trailing 's' so that newlines won't be given special treatment, if these are strings that can contain newlines), but again, this depends heavily on which quoted format you're parsing from — if C-style escapes like \n or \t are allowed, or if you have quote-doubling as in DOS/VB strings, then things get More Interesting.die "not quoted?" unless $ptext =~ m/\A(["'])(.*)\1\z/s; $praw = $2; $praw =~ s/\\(.)/$1/sg
and then once you've got the actual $praw you can do
plus whatever other junk you want in the regexp.$pregexp = qr/\Q$praw\E/;
In reply to Re: Regexp and metacharacters
by wrog
in thread Regexp and metacharacters
by Largins
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |