home | list info | list archive | date index | thread index

Re: [OCLUG-Tech] Perl regex problem

On Mon, Mar 19, 2007 at 07:08:58PM -0400, Milan Budimirovic wrote:
> I want to do a systematic search and replace of a word only where the 
> word is NOT enclosed in double quotations.

#!/usr/bin/perl -w
while(<>){
        # surround quoted strings with unprintables
        s/("[^"]+")/\177$1\177/g;
        # split on the surrounded strings
        @a = split(/\177/);
        # loop over parts and avoid quoted pieces
        foreach $s ( @a ) {
                next if $s =~ /"/;
                $s =~ s/foo/bar/g;
        }
        # re-assemble and print
        print join("",@a);
}


-- 
| Ian! D. Allen  -  idallen [ at ] idallen [ dot ] ca  -  Ottawa, Ontario, Canada
| Home Page: http://idallen.com/   Contact Improv: http://contactimprov.ca/
| College professor (Open Source / Linux) via: http://teaching.idallen.com/
| Defend digital freedom:  http://eff.org/  and have fun:  http://fools.ca/

replies

references

message navigation