I'd be inclined to do it like this:
my $string = 'one two "three" four "five" six'; my(@quot, @unquot); while ($string =~ m{ \G (?: ([^\s"]+) # $1: unquoted string, never empty | " ([^"]*) " # $2: quoted string, maybe empty ) \s* # consume the separating whitespace }xgc) { if (defined $1) { push @unquot, $1; } else { push @quot, $2; } } unless (pos($string) == length($string)) { warn "Couldn't parse string - maybe the quotes are mismatched\n"; } print "quoted strings: @quot\n"; print "unquoted strings: @unquot\n";
This keeps the parsing in one place, so you can easily (for example) extend the matching to allow backslash-escaped quotes inside the quoted strings.
HugoIn reply to Re: separated quoted stings from unquoted strings
by hv
in thread separated quoted stings from unquoted strings
by Cody Pendant
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |