use Math::Combinatorics qw( permute ); use Regexp::List qw( ); my @charset = qw( a b c ); my ($valid) = map qr/^$_\z/, Regexp::List->new()->list2re(permute(@charset)); my ($others) = map qr/[^$_]/, join '', map quotemeta, @charset; sub match { my $s = shift; $s =~ s/$others//g; return $s =~ $valid; }