$string = 'one two "three" four "five" six'; $count_quotes = $string =~ tr/"/"/; if($count_quotes && ($count_quotes % 2 != 0)){ # there is an odd number of quotes in the string $quotedstringerror = 1; # use this to show user an error message later }elsif($count_quotes){ # there is an even number of quotes in the string $quotedstrings = 1; } if($quotedstrings){ @quotedstrings = $string =~ /"([^"]*?)"/g; # get them all out with m// $string =~ s/"[^"]*?"//g; # remove them from the original with s/// } @unquotedstrings = split(/ +/,$string); # grab remaining regular strings using split print "\@quotedstrings: @quotedstrings \n"; print "\@unquotedstrings: @unquotedstrings \n"; # output: @quotedstrings: three five # @unquotedstrings: one two four six