#!/usr/bin/perl use strict; use Data::Dumper; my $line = q[keyword1 value keyword2 "value with spaces" keyword3 value]; print Dumper tokenize_line($line); sub tokenize_line { my $line = shift; my @tokens; while ($line =~ /(\S+)/g) { # every non-space match is a token push @tokens, $1; # anything in double-quotes is a single token if ($line =~ /\G\s*"(.+?)"/) { push @tokens, $1; # continue from this last match $line = $'; } } return \@tokens; }