in reply to Look Behind not work, please help


Perhaps a simple negated character class and capture might work just as well:
#!/usr/bin/perl -w use strict; my $str = 'ABC"123"XYZ'; my @p = split /("[^"]+")/, $str; foreach my $part (@p) { print "part = ($part)\n"; } __END__ Prints: part = (ABC) part = ("123") part = (XYZ)

--
John.

Replies are listed 'Best First'.
Re: Re: Look Behind not work, please help
by halley (Prior) on Apr 30, 2003 at 14:59 UTC

    That's an interesting technique.

    The split() call normally captures everything that doesn't match the regex; in this case, it's the unquoted parts. You add on a capture in the regex, which tells split to also keep the separators; in this case, it's the quoted parts. The non-obvious part is that split will interleave the captures-in-separators and the usual non-separator portions properly.

    --
    [ e d @ h a l l e y . c c ]


      Yes, it is one of the many interesting features of split, from the pod:
      If the PATTERN contains parentheses, additional list elements are created from each matching substring in the delimiter. split(/([,-])/, "1-10,20", 3); produces the list value (1, '-', 10, ',', 20)

      Other interesting features of split are LIMIT, negative LIMIT, the parameter-less split, the difference between ' ', / / and /\s+/ and the implicit split to @_ when not in a list context (which is very useful in Golf).

      I used one of these features in Strip leading and trailing whitespace from a string..

      --
      John.