use strict; use warnings; my $regex = qr/ ([^,]+) (?=\Z|,) /x; my $var = 'abcd,ef,,ghijkl,mnop'; print "$1\n" while $var =~ /$regex/g;