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

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.