Re: How to match a token not be quoted?



On Jan 28, 2008 9:09 PM, Zhao, Bingfeng <Bingfeng.Zhao@xxxxxx> 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?
.



Relevant Pages

  • Re: Splitting a string with Regex and keep the separator
    ... I want also to thank you for the regex explanation. ... a key/val pair. ... If this group captures multiple tokens they're added to the ... is made up of one or more alphanumeric characters. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: regex help partial code
    ... I have with great effort started on teh regex implemention and am ... the second token can't have whitespace. ... characters at the end, but your test didn't have any. ... Though, if there can't be whitespace in any of the tokens, you might ...
    (comp.lang.ruby)
  • Re: My first Python program -- a lexer
    ... I had been thinking about further reducing the number of regex matchings needed. ... So I wanted to modify my lexer not to tokenize the whole input at once, but only try to grab the next token from the input "just in time" / on demand. ... [Optimizing performance by putting most frequent tokens first] ... Still, I should be able to gain even better performance with my above approach using a nextfunction, as this would completely eliminate all "useless" matching (like trying to match FOO where no foo is allowed). ...
    (comp.lang.python)
  • Re: preg_match at offset
    ... input string and the tokens array in the constructor. ... regex would be more suitable: ... From personal experience, PHP performs very well ...
    (comp.lang.php)
  • Re: preg_match at offset
    ... I want to split a given string into tokens which are defined by regexes: ... regex would be more suitable: ... can contain assertions such as ^, ...
    (comp.lang.php)