use strict; my (@Keys,@KeysNeed,@KeysAvoid); my $keywords = qq(word +"my phrase" -"my phrase's mom" +other keywords); while ($keywords) { $keywords =~ s/^\s*([+-]*)(((["'])(.*?)\4)|\S+)\s*//; my $cat = $1; my $keyword = $2; $keyword =~ s/^[^\w\s]+//g; # Strip special chars from the ends $keyword =~ s/[^\w\s]+$//g; next unless $keyword; if ($cat =~ /\+/) { push @KeysNeed, $keyword; } elsif ($cat =~ /\-/) { push @KeysAvoid, $keyword; } else { push @Keys, $keyword; } } print "KEYS NEEDED: @KeysNeed\n"; print "KEYS TO AVOID: @KeysAvoid\n"; print "KEYS: @Keys\n"; # prints: # KEYS NEEDED: my phrase other # KEYS TO AVOID: my phrase's mom # KEYS: word keywords