Welcome to the Monastery | |
PerlMonks |
Matching permutations with regexby QM (Parson) |
on Nov 20, 2018 at 11:29 UTC ( [id://1226058]=perlquestion: print w/replies, xml ) | Need Help?? |
QM has asked for the wisdom of the Perl Monks concerning the following question:
Today I learned that a regex can match permutations of a given string.
Update: Oops, I had the terms reversed on the regex, it should be $i =~ $x .... I've corrected it, but it doesn't really change the results. For single characters without repeats, such as any permutation of 'abc', this seems the shortest, most readable method:
which outputs:
For single character, with limited repeats, such as any permutation of 'aabbcc', a slightly different approach is needed. I tried the following, but it matches almost everything. I suspect there's some unexpected behavior in the backtracking?
which outputs 540 of the 728 strings given (at least 1 of each char is present):
Is there some other magic to DWIM? For nonrepeating, multichar permutations, even when individual chars are shared between tokens, this approach works:
which outputs:
I found this here on Stack Overflow. -QM -QM
Back to
Seekers of Perl Wisdom
|
|