abc "a\"bc$xy\\z$abc$x­$y\\" $bla'h, "$baz" # ^^ #### $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;