That doesn't deal with double backslashes properly. Consider:

abc "a\"bc$xy\\z$abc$x­$y\\" $bla'h, "$baz" # ^^
the tagged sequence will be interpretted as an escaped quote when it is really an escaped backslash followed by an unescaped quote. So $bla will be changed to \$bla when it really shouldn't be.

You might look at Regex::Common, but I suspect it might not be helpful. (:

In any case, here is how I'd solve it:

$str =~ s[ \G # Don't skip anything ( # $1 is unquoted text (?: [^\\"]+ # Uninteresting stuff | \\. # Escaped character )* )( # $2 is quoted text " # Opening quote (?: [^\\"]+ # Unintersting stuff | \\. # Escaped character )* ("?) # $3 is closing quote ) ][ die "Unclosed quote ($2)" unless $3; my $q= $2; $q =~ s[\$][\\\$]g; $1 . $q; ]xseg;
but change the second [^\\"] and \\. to [^\n\\"] and \\[^\n] if you don't want to allow quoted strings to contain newlines.

Updated to address Aristotle's point about escaped quotes outside of quoted strings, though that assumes that such is possible (it makes sense for a shell script but not for Perl code).

Also note that eyepopslikeamosquito now says that neither of these ever happen. Oh, well.

Update2: Thanks, eyepopslikeamosquito. Sorry I didn't get a chance to test the code and I'm glad you were able to figure out what was wrong with it.

                - tye

In reply to Re^2: Escape $ inside quotes only (\\) by tye
in thread Escape $ inside quotes only by eyepopslikeamosquito

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.