in reply to Splitting without losing
That is, use both look-behind and look-ahead assertions, both zero-width. (update: made the above line coincide with the tested code below) I tried the following snippet on the command line to verify that:split(/(?<=\S)(?=\s)/);
Output:$s="AAA B X Y Z W\n"; @a=split(/(?<=\S)(?=\s)/, $s); print length($s),":$s"; print join(":",length(join("",@a)),scalar(@a),"\n"); print join("\n",@a),"\n";
Note that this treats the final LF (or CRLF, if that's your flavor) as a token -- the second line of output shows that the array got seven elements, while the string had just six non-whitespace tokens followed by LF.20:AAA B X Y Z W 20:7: AAA B X Y Z W
|
|---|