Re: sed: How to avoid making changes within a literal string?
On 24 Aug 2006 23:23:40 -0700, Xicheng Jia wrote:
[...]
> bash: ~$ echo '
> if not ( ( a and b ) or ( c and d ) )
> write(output) << "Send receipt\" and sample \"of blood to
> rebateshq.com
> and expect not to get a reply."
> ' | perl -0777pe 's/("[^\\"]*(?:\\.[^\\"]*)*")|and/$1 or "&&"/eg'
>
> if not ( ( a && b ) or ( c && d ) )
> write(output) << "Send receipt\" and sample \"of blood to
> rebateshq.com
> and expect not to get a reply."
> _______________________________
> Just change
>
> "[^"]*"
>
> to
>
> "[^\\"]*(?:\\.[^\\"]*)*"
>
> or change it to
>
> "(?:\\.|[^\\"]*)*"
>
> the latter one is much easier to be understood but less efficient from
> regex's application viewpoint.
Why? They look mostly equivalent to me.
Or
"(?:\\.|.)*?"
$ echo 'a and b "c and \"and\" d" and e' |
perl -0777 -pe 's/("(?:\\.|.)*?")|and/$1 or "&&"/ge'
a && b "c and \"and\" d" && e
If the quote is not matched (as in 'foo "and'), there will be
substitution with either solution.
--
Stephane
|