foreach my $word (split ' ', $match) { if ($word =~ /^V(?:DD|CC)\z/) { # push @PN, etc. } elsif ($word =~ /^(?:VSS|GND)\z/) { # push @GN, etc. } }