in reply to splitting punctuation in a text

If you have capturing parentheses in the regex in split, the stuff in there comes out in the resulting list.

use Data::Dumper; my $s = 'earth, wind & fire'; my @out = split /\s*([,&])\s*/, $s; print Dumper \@out; __END__ $VAR1 = [ 'earth', ',', 'wind', '&', 'fire' ];

Of course, make "[,&]" the class of punctuation you actually care about.

Replies are listed 'Best First'.
Re^2: splitting punctuation in a text
by Your Mother (Archbishop) on Aug 11, 2008 at 17:38 UTC

    This also might be a good modification-

    my @out = split /\s*([[:punct:]])\s*/, $s;

    Depending on input and what the OP needs in the end. I think the POSIX classes, like punct, came in with 5.6. Someone will correct me if that's not right.