Re: Matching at least $x words out of @words

"A. Pagaltzis" <[email protected]>
Newsgroups gmane.comp.lang.perl.fun
Message-ID <20050506000502.GA2120@klangraum>
* José Castro <[email protected]> [2005-05-05 16:30]:
> 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?

    $alt = join '|', qw( word1 word2 word3 );
    / ($alt) .* ($alt) (?(?{ $1 eq $2 })(?!)) /x

Note:
It can probably be reduced further to remove the need for $alt
duplication in the regex. The only way I can see would entail
using (?{}) and local to save matches, though.

Warning:
Lots of backtracking. Use a grep solution as shown by others in
pratice.

Regards,
-- 
#Aristotle
*AUTOLOAD=*_=sub{s/(.*)::(.*)/print$2,(",$\/"," ")[defined wantarray]/e;$1};
&Just->another->Perl->hacker;
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.