BUU has asked for the wisdom of the Perl Monks concerning the following question:

How would i construct a regex, to feed to split i would guess, so i could split on any pipe thats not contained by brackets? Ex.:if my original string was "foo|{baz|qux}|fax" i would want my three elements to be "foo","{baz|qux}" and "fax".

So far i looked at the negative look ahead and negative look behind expressions, and i came up with something that doesnt work, namely
/ (?<! # start the negative look behind \{ # an opening bracket .* # anything else ) # close negative look behind \| # the pipe (?! # start the negative look ahead .* # anything.. \} # followed by a bracket ) # close the look ahead /x
Obviously the .* is going to break things and so forth, so.. what can i change?

Replies are listed 'Best First'.
Re: Split unless contained by
by dws (Chancellor) on Aug 09, 2002 at 23:28 UTC
    How would i construct a regex, to feed to split i would guess, so i could split on any pipe thats not contained by brackets?

    You needn't use split(). A regex is sufficient:

    my $str = "foo|{baz|qux}|fax"; my @parts = $str =~ /( \{.+?\} # e.g., {baz|qux} | [^|]+ # e.g., foo ) (?:\||$) # |, or end of string /xg; $" = "\n"; print "@parts"; __END__ foo {baz|qux} fax

      Hah. Caught using a pair of pliers when I needed a hammer. Hats off to you, dws and belkajm.

      Indeed, ++ to dws, this works perfectly, my thanks.
Re: Split unless contained by
by Ferret (Scribe) on Aug 09, 2002 at 22:56 UTC

    Starting from the premise that a lame but almost working answer is better than none:

    /( #A nice simple capture, for [^|\{]+ #a block of nonsignificant chars | \{ [^}]+ \} #or a bracketed block ) \| #followed by a pipe. /x

    Doesn't _quite_ work - produces the following on output:

    $VAR1 = [ '', 'foo', '', '{baz|qux}', 'fax' ];

    This indicates my need for enlightenment - if I use (?:) instead of (), it returns just (' ',' ','fax'), not the captures... so maybe there's some deeper magic going on in split which I'm not clear on.

    Update: Found why it's adding the captured elems, but not why using (?:) breaks it.

    from split manpage:

    If the PATTERN contains parentheses, additional list elements are crea +ted from each matching substring in the delimiter.

    Update again: Now I fool feelish. Okay, yup, like would make sense, split uses pre/postmatch stuff - the end result being that since (?:) is part of the match, it gets dropped, and since that's the content I'm looking for (the foo or {bar|baz} bit), I lose my content.

    hm. now I'm as stumped as BUU. Can't use lookbehinds, cuz variable length lookbehinds aren't implemented... *falls into contemplative silence*.

    But is that approximately what you're looking for?

      if you modify this slightly, and use it in this form it seems to work:
      $a = "foo|{baz|qux}|fax"; @b = ($a =~ /( [^|\{]+ | \{ [^}]+ \} ) (?: \| | $ ) /xg);
      @b now contains:
      $VAR1 = [ 'foo', '{baz|qux}', 'fax' ];
      Note that this regexp can't cope with recursive brackets, and can therefore be fooled. Any example of this would be:
      $a = "foo|{baz|{qu}x|fax";
      which would result in:
      $VAR1 = [ 'foo', 'baz', 'qu}x}', 'fax' ];

      I'm afraid that I haven't got round to getting the owl book yet, so i'm not really that hot with regexps yet. In fact, i'm coming back to perl now from a years absence. If someone could show how to modify the regexp to cope with things like this, I would be grateful.

      Yoda