Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: REgular expression to check the string that allows "a","b" and "c" to occur only once in any order. (permutations)

by ikegami (Patriarch)
on Dec 11, 2007 at 06:47 UTC ( [id://656325]=note: print w/replies, xml ) Need Help??


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; }
  • Comment on Re^2: REgular expression to check the string that allows "a","b" and "c" to occur only once in any order. (permutations)
  • Select or Download Code

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

    True. The OP's spec wasn't clear as to whether or not the strings could contain other characters (or were limited to only those in the specified character set), or if a, b, and c were really characters and not stand-ins for longer strings. I went with Occam's razor and took the OP literally. (Update: Since the list of invalid strings contained only a, b, and c; I assumed if other characters were possible there would be an example of it in the invalid list. ikegami is right, though - this was probably more of an assumption than I first thought.)

    This approach could also be problematic if the character set is large and/or if the wanted strings were significantly longer, since the number of permutations would rapidly increase. In that case, a RE-based approach may be better.

    Nonetheless, thanks for providing a more robust solution.

      I went with Occam's razor and took the OP literally.

      No, that's what I did. You added the clause "and no characters other than a, b and c".

      It could very well be the OP wants what you provided, but it's not a literal interpretation of the OP.

      The authors of the other two (working) solutions didn't add that clause.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://656325]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-25 22:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found