# $re = exclude(@words); sub exclude { my %words; push @{ $words{ quotemeta substr($_, 0, 1) } }, quotemeta substr($_, 1) for @_; my $first = "[^@{[ join '', keys %words ]}]*"; my $rest = join "|", map "$_(?!" . join("|", @{ $words{$_} }) . ")", keys %words; return qr/^$first(?:(?:$rest)$first)*$/; } my $re = exclude(qw( this that those )); # print $re; # for debugging purposes for ("I like this", "give me that one", "these rock!") { print "$_ => " . /$re/; }