use strict; sub main{ my $text = qq{a,b,"hey, you","str1, str2, str3",end}; print "Input: $text\n\n"; # Split the delimiters my @values = split( /(?:\,|(\".*?\"))/ , $text); # Remove the created blanks @values = grep{$_ ne ''} @values; # Output foreach (0..$#values){ print "$_: $values[$_] \n"; } } main(); #### Input: a,b,"hey, you","str1, str2, str3",end 0: a 1: b 2: "hey, you" 3: "str1, str2, str3" 4: end