in reply to Escape $ inside quotes only

Here you go:

$str=~s/("(\\\"|[^"])+")/{$_=$1;s|\$|\\\$|g;$_}/ge;

One-liners...ya can't beat em with a stick:D

Good luck!

Replies are listed 'Best First'.
Re^2: Escape $ inside quotes only (\\)
by tye (Sage) on May 07, 2003 at 06:25 UTC

    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

      Very nice solution! Your interpretation of quoting is correct (I blundered before). Your solution does not work, as is, however, because $1 gets clobbered. The following minor changed version of your solution does work for me (note also the extra line of test data with your quoting example).

      my $str = <<'EOS'; abc "a\"bc$xyz$abc$x$y" $bla'h, $blah hello k\$nob this is "\"'abc$xy$z\"" test "$" a$$bc "$$abc$xyz$abc$" blah, $$, blah abc "a\"bc$xy\\z$abc$x$y\\" $bla'h, "$baz" EOS print "$str\n\n"; $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 $t = $1; # changed line my $q = $2; $q =~ s[\$][\\\$]g; $t . $q; # changed line ]xseg; print $str;

      which prints:

      abc "a\"bc$xyz$abc$x$y" $bla'h, $blah hello k\$nob this is "\"'abc$xy$z\"" test "$" a$$bc "$$abc$xyz$abc$" blah, $$, blah abc "a\"bc$xy\\z$abc$x$y\\" $bla'h, "$baz" abc "a\"bc\$xyz\$abc\$x\$y" $bla'h, $blah hello k\$nob this is "\"'abc\$xy\$z\"" test "\$" a$$bc "\$\$abc\$xyz\$abc\$" blah, $$, blah abc "a\"bc\$xy\\z\$abc\$x\$y\\" $bla'h, "\$baz"

      as does my original attempt; however, the last line of this new test data trips up the earlier one liner.

Re^2: Escape $ inside quotes only (not the solution)
by Aristotle (Chancellor) on May 07, 2003 at 06:12 UTC
    Nope, doesn't work.
    local $_ = q[abc \"$a"bc$xyz$abc$x$y" $bla'h]; s/("(\\\"|[^"])+")/{$_=$1;s|\$|\\\$|g;$_}/ge; print; __END__ abc \"\$a"bc$xyz$abc$x$y" $bla'h

    Makeshifts last the longest.

Re: Re: Escape $ inside quotes only
by eyepopslikeamosquito (Archbishop) on May 07, 2003 at 05:03 UTC

    Yep, that works for me. Thanks.