in reply to Re: REgular expression to check the string that allows "a","b" and "c" to occur only once in any order. (permutations)
in thread REgular expression to check the string that allows "a","b" and "c" to occur only once in any order.
It fails when you add adbc to @match_wanted. Easily fixed:
use Math::Combinatorics qw( permute ); my @charset = qw( a b c ); my %valid = map { join( '', @$_ ) => 1 } permute( @charset ); my ($others) = map qr/[^$_]/, join '', map quotemeta, @charset; sub match { my $s = shift; $s =~ s/$others//g; return $valid{$s}; }
or
use Math::Combinatorics qw( permute ); my @charset = qw( a b c ); my ($valid) = map qr/^(?:$_)\z/, join '|', map quotemeta, map join('', + @$_), permute @charset; my ($others) = map qr/[^$_]/, join '', map quotemeta, @charset; sub match { my $s = shift; $s =~ s/$others//g; return $s =~ $valid; }
or
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(@cha +rset)); my ($others) = map qr/[^$_]/, join '', map quotemeta, @charset; sub match { my $s = shift; $s =~ s/$others//g; return $s =~ $valid; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: REgular expression to check the string that allows "a","b" and "c" to occur only once in any order. (permutations)
by bobf (Monsignor) on Dec 11, 2007 at 06:55 UTC | |
by ikegami (Patriarch) on Dec 11, 2007 at 07:03 UTC |