A better way to achieve that goal is with a 'character class', where you'd write: /[ab]+/
Regarding matching p.*e.*r.*l, the various solutions so far lack 'data driven'-ness, in that they embed the sought-after characters in code.
An approach which allows the sought-after chars to be specified would be:
A more insane (and presumably less efficient) way of doing it would be to compile the N sought after characters into a regexp which would match each of the N! ways these chars might occur in the string.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/; }
In reply to Re: A Quick Regex Question
by jbert
in thread A Quick Regex Question
by chinamox
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |