use strict; while () { chomp $_; my @fields = /([^\s"]+|(?:[^\s"]*"[^"]*"[^\s"]*)+)(?:\s|$)/g; print "$_\n" for @fields; print "\n"; } __DATA__ 1 2 3 4 5 1 "2 3" 4 5 1 2" 3 " 4 5 1 2" 3 "4 5 1 2" 3 "4" 5" 6 1 2" 3 "4"" 5"" 6