On Jan 28, 2008 9:09 PM, Zhao, Bingfeng <[EMAIL PROTECTED]> wrote:
> Hello,
> I want to a cure regex that match following requirements: given $line =
> 'abc abc "abc abcc" abcc', I want to replace all instances of "abc" that
> not in quotation with, say 'd', so I expect I get 'd d "abc abcc" dc'.
> What should I write my regex? I try some and referred cook book also, no
> solution. Thanks in advance.
snip

While this can be done, it is often easier to understand the following code

#!/usr/bin/perl

use strict;
use warnings;

my $string = 'abc abc "abc abcc" abcc';

my @tokens = split /(")/, $string;
my $in_string = 0;
for my $token (@tokens) {
        $in_string = not $in_string if $token =~ /"/;
        next if $in_string;
        $token =~ s/abc/d/g;
}
$string = join "", @tokens;

print "$string\n";

If you really need to do it with a regex then I would consult perldoc
-q balanced and
http://perldoc.perl.org/perlfaq6.html#Can-I-use-Perl-regular-expressions-to-match-balanced-text?

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to