in reply to Simple regex wordlist question

What you want is "The currect dict word contains the first letter, the currect dict word contains the second letter, ... and the currect dict word contains the last letter." Regexp are not good at doing "AND". It's usually better to do multiple matches, and AND the results of the matches.

Update: No, I suppose that's not quite right. You want "All letters of the currect dict word is in the specified word." A regexp character class can handle that quite well.

use strict; use warnings; my $input = <STDIN>; my ($re) = map qr/^[$_]*$/, join '', map quotemeta, $input =~ /(.)/g; my $dict = 'wordlist.txt'; open(my $dict_fh, '<', $dict) or die("Unable to open dictionary file: $!\n"); while (<$dict_fh>) { print if /$re/; }

Update: Note that the OP didn't mention each letter could only be used once, and he didn't mention that all the letters needed to be used, so I didn't add those restrictions.

Replies are listed 'Best First'.
Re^2: Simple regex wordlist question
by goibhniu (Hermit) on Sep 11, 2007 at 16:00 UTC

    I think he is working on the puzzle where you think of as many words that can be spelled using letters in the given word. But what about quantity?

    I've already given ++ to Zaxo and CountZero for their anagram solutions, but I'm not sure now that's the point

    An example would help, but if I'm right, he wants, given 'PerlMonks' a list like:

    • Perl (of course)
    • role
    • Poke
    • sole
    • solemn
    But would not like 'reel', as there are not two 'e's in the input.

    Is this what johngg has?

    perhaps vcTheGuru is closer to right with Math::Combinatorics, except maybe using the combine function looping from 1..($#input+1).


    I humbly seek wisdom.

      The following needs a great deal of polish. I took ikegami's basic regexp as a superset of the words you're looking for. Then I horribly misused it without all the safety that ikegami was right to include (qr// syntax, etc.).

      I also ended up turning your semantic inside out in that the dictionary file is now on the command line and the starting word is hard coded; sorry about that.

      The guts of this solution involve counting letters in a regexp:


      I humbly seek wisdom.