my @required_chars = qw/p e r l/; # Or anything else... LINE: while (<>) { foreach my $char (@required_chars) { next LINE unless index($_, $char) >= 0; } print; } #### #!perl -w # Warning - silly way of solving problem use strict; # CPAN, how we love thee use Math::Combinatorics; my @required_chars = qw/p e r l/; # Or anything else... # For bonus points, we could create a package which blessed # the regexp into an object. But that would be silly. my $re = make_re(@required_chars); while (<>) { print if /$re/; } sub make_re { # Get all N! ways of arranging the chars my @combinations = permute(@_); # Construct a.*b.*.*d strings, for each my @res = map { join(".*", @$_); } @combinations; # Put them together into a honking great regexp my $re = "(" . join("|", @res) . ")"; return qr/$re/; }