use strict; use warnings; my $test = 'abc , def'; $test =~ /([\s\w]+),([\s\w]+)/; for ($1, $2) { my $string = $_; $string =~ s/\s+\z//ms; print "$string\n"; }