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

  1. parse a string from some kind of quoted format and then
  2. make a regexp out of it....

... 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

die "not quoted?" unless $ptext =~ m/\A(["'])(.*)\1\z/s; $praw = $2; $praw =~ s/\\(.)/$1/sg
(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.

and then once you've got the actual $praw you can do

$pregexp = qr/\Q$praw\E/;
plus whatever other junk you want in the regexp.

In reply to Re: Regexp and metacharacters by wrog
in thread Regexp and metacharacters by Largins

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.