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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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:

@x = glob('{a,b,c}'x3); # for testing: all permutations, plus many mor +e $x = '(?:([a-c])(?!.*\1)){3}'; # regex for $i (@x) { $i =~ $x and say $i; # was $x =~ $i }

which outputs:

abc acb bac bca cab cba

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?

@x = glob('{a,b,c}'x6); $x = '(?:a()|a()|b()|b()|c()|c()){6}\1\2\3\4\5\6'; # limited repeats a +llowed for $i (@x) { $i =~ $x and say $i; # was $x =~ $i }

which outputs 540 of the 728 strings given (at least 1 of each char is present):

aaaabc aaaacb aaabac aaabbc aaabca ... cccbba cccbca ccccab ccccba

Is there some other magic to DWIM?

For nonrepeating, multichar permutations, even when individual chars are shared between tokens, this approach works:

@x = glob('{abc,bcd,cde}'x3); # permutations of 'abc/bcd/cde' $x = '(?:abc()|bcd()|cde()){3}\1\2\3'; for $i (@x) { $i =~ $x and say $i; # was $x =~ $i }

which outputs:

abcbcdcde abccdebcd bcdabccde bcdcdeabc cdeabcbcd cdebcdabc

I found this here on Stack Overflow.

-QM
--
Quantum Mechanics: The dreams stuff is made of

-QM
--
Quantum Mechanics: The dreams stuff is made of


In reply to Matching permutations with regex by QM

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-18 15:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found