Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re^2: Matching permutations with regex

by QM (Parson)
on Nov 22, 2018 at 09:20 UTC ( #1226170=note: print w/replies, xml ) Need Help??

in reply to Re: Matching permutations with regex
in thread Matching permutations with regex

OK. That's probably pretty fast too.

But I think you misunderstood? I can generate them, but I wanted to _match_ any permutation of a given string.

Update: Oh, I had the =~ backwards, and that led you astray.

(Not shown in the OP is a programmatic way to generate the regex solution based on the input string, but that was left out for clarity.)

Quantum Mechanics: The dreams stuff is made of

Replies are listed 'Best First'.
Re^3: Matching permutations with regex
by tybalt89 (Prior) on Nov 22, 2018 at 15:52 UTC

    Like so ?

    #!/usr/bin/perl -l # use strict; use warnings; $_ = 'aabbcc'; my $len = length; my $inner = join ',', split //, tr///csr; my $glob = "{$inner}" x $len; my $re = "^(?=.{$len}\$)"; while( /./ ) { my $count = s/$&//g; $re .= "(?=(?:.*$&){$count})"; } $_ =~ $re and print for glob $glob;

    Outputs the 90 valid permutations.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2021-01-27 07:54 GMT
Find Nodes?
    Voting Booth?