in reply to Re: Escape $ inside quotes only
in thread Escape $ inside quotes only
That doesn't deal with double backslashes properly. Consider:
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.abc "a\"bc$xy\\z$abc$x$y\\" $bla'h, "$baz" # ^^
You might look at Regex::Common, but I suspect it might not be helpful. (:
In any case, here is how I'd solve it:
but change the second [^\\"] and \\. to [^\n\\"] and \\[^\n] if you don't want to allow quoted strings to contain newlines.$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;
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re^2: Escape $ inside quotes only (\\)
by eyepopslikeamosquito (Archbishop) on May 07, 2003 at 07:24 UTC |