eyepopslikeamosquito has asked for the wisdom of the Perl Monks concerning the following question:

Want to convert this:

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

to:

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

That is, escape (with \) all $ but only if they occur inside double-quoted strings. Here is my attempt:

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 EOS print "$str\n\n"; # $qq matches a double quoted string. my $qq = qr/"[^"\\]*(?:\\.[^"\\]*)*"/; # Break $str into fields (quoted or unquoted). my @fields = $str =~ /$qq|[^"]+/g; # Escape $ in quoted fields only. /^"/ && s/\$/\\\$/g for @fields; print @fields;

Improvements/alternative approaches welcome.

Replies are listed 'Best First'.
Re: Escape $ inside quotes only
by pzbagel (Chaplain) on May 07, 2003 at 04:18 UTC

    Here you go:

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

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

    Good luck!

      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.

      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.

      Yep, that works for me. Thanks.

Re: Escape $ inside quotes only
by BrowserUk (Patriarch) on May 07, 2003 at 03:56 UTC

    Here's an alternative, whether it's an improvement is your call:)

    $str =~ s[("[^"]*?(?<!\\)")] { (my $temp = $1) =~ s[(?<!\\)\$][\Q\$]g; $temp; }mge;

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

      This fails for me with multiple escaped " inside string. For example:

      my $str = <<'EOS'; "\"$x\"" EOS $str =~ s[("[^"]*?(?<!\\)")] { (my $temp = $1) =~ s[(?<!\\)\$][\Q\$]g; $temp; }mge; print $str;

      does not escape the $.

Re: Escape $ inside quotes only
by Abigail-II (Bishop) on May 07, 2003 at 14:34 UTC
    s/([^"\\]+|\\.)|("(?:[^"\\]+|\\.)*")/ defined $1 ? $1 : do {local $_ = $2; s'\$'\$'g; $_}/ge;

    Abigail

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

    I got a question on chatterbox, but I don't know how to chatter yet (sorry 'bout that). Anyhow, to clarify, if a solution passes the example test data above, it is probably ok. If $ occurs inside quoted string, it is to be replaced by \$ unilaterally; \ is only treated specially inside a quoted string (and only then to escape "). Sorry if that makes it too easy. :-)