in reply to Re: Complex Splitting
in thread Complex Splitting

I don't think it's a lot trickier. This oughta do it:
@array = grep defined, split /(\[.*?\])|/;
That is, if you split on a bracketed group, capture it. If you don't have a bracketed group, split on nothing. Then filter out where the capture didn't match.

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^3: Complex Splitting
by johngg (Canon) on Feb 06, 2007 at 18:29 UTC
    Yes, I regretted the word "lot" as soon as I'd posted, made it sound as if I'd sweated blood writing it. Although I like the simpler approach of your solution there is one problem with it; it doesn't get rid of the square brackets, which was required by the OP.

    Cheers,

    JohnGG

      Hasty reading on my part. Not capturing the brackets is as simple as leaving them outside the capturing parentheses:
      @array = grep defined, split /\[(.*?)\]|/;

      Caution: Contents may have been coded under pressure.