in reply to •Re: split and capture some of the separators
in thread split and capture some of the separators

I did think about that, but i cant quite see how to do it in this case. Part of the problem for me is that the separators are well-defined, but what between them could be anything (except a separator).
  • Comment on Re^2: split and capture some of the separators

Replies are listed 'Best First'.
Re^3: split and capture some of the separators
by bart (Canon) on Oct 07, 2004 at 22:18 UTC
    You can use the following "continue to split" mechanism in a loop:
    (my($token, $sep), $string) = split /PATTERN/, $string, 2;
    This will load the matched string (the separator) into $sep, the stuff before that into $token, and the rest of the string right after the match, into the string, shortening it, ready for the next iteration — provided you have exactly one pair of capturing parens in the pattern.

    It's almost identical in effect (bar the negative impact on the global speed of regexes) as using the special variables $`, $&, $' on a normal match, using the same pattern.

    If you could have more capturing parens, you can do:

    my($token, @sep) = split /PATTERN/, $string, 2; $string = pop @sep;
    leaving all the captured separators in @sep.
Re^3: split and capture some of the separators
by Jasper (Chaplain) on Oct 08, 2004 at 09:14 UTC
    Part of the problem for me is that the separators are well-defined, but what between them could be anything (except a separator).

    This looks (to me) like that sentence written in perl:
    @list = /([SEPARATORS])+([^SEPARATORS])*/g;
    You could include \s in the second character class if you wanted to ignore whitespace.

    Of course, I've been wrong in the past :)