in reply to Re^2: split and capture some of the separators
in thread split and capture some of the separators
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.(my($token, $sep), $string) = split /PATTERN/, $string, 2;
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:
leaving all the captured separators in @sep.my($token, @sep) = split /PATTERN/, $string, 2; $string = pop @sep;
|
|---|