Matching at least $x words out of @words

[email protected] (José Castro)
Newsgroups perl.fwp
Message-ID <[email protected]>
Hi, folks.

So suppose you want a regular expression to match at least one of
three words:

/word1|word2|word3/

What solution would you use if you wanted at least _two_ of those
three words?


Isn't there a neater (and probably twisted, of course) way of saying
/match at least $x of the words in @words/ without having to do
something like:

1) get all the combinations of $x words in @words
2) for each of those, find the permutations ( [qw/word1, word2/],
  [qw/word2, word1/] )
3) create the regexen ( /word1.*word2/, /word2.*word1/ )
4) join them all with "|" and create the regex out of it

Something in the lines of:

join "|", map { join ".*", @$_ } map { [permutations(@$_)] } combinations($x, @words)

(code untested, of course)

There's gotta be a better way, no? Any comments?

Cheers,

jac

--
Jose Alves de Castro <[email protected]>
      http://jose-castro.org/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.