Re: How to match a token not be quoted?
- From: chas.owens@xxxxxxxxx (Chas. Owens)
- Date: Mon, 28 Jan 2008 21:35:15 -0500
On Jan 28, 2008 9:09 PM, Zhao, Bingfeng <Bingfeng.Zhao@xxxxxx> wrote:
Hello,snip
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.
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?
.
- References:
- How to match a token not be quoted?
- From: Bingfeng Zhao
- How to match a token not be quoted?
- Prev by Date: Re: How to match a token not be quoted?
- Next by Date: Re: How to match a token not be quoted?
- Previous by thread: Re: How to match a token not be quoted?
- Next by thread: Re: How to match a token not be quoted?
- Index(es):
Relevant Pages
|