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

[email protected] (Philippe 'BooK' Bruhat)
Newsgroups gmane.comp.lang.perl.fun
Message-ID <[email protected]>
Le jeudi 05 mai 2005 à 15:24, José Castro écrivait:
> 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?

    my @words = qw( foo bar foobar );
    $re = join '', map { '(?=.*(' . (quotemeta) . '))?' } @words;
    $re = qr/$re/;
    my @matches = grep defined, "foo bar baz" =~ $re;

The capturing parent allow you know which word was matched.

-- 
 Philippe "BooK" Bruhat

 He who revels in being bigger forgets that he is the larger target.
                                                 (Moral from Groo #5 (Image))
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.