Thanks all.
On 12 Apr 2006 06:23:53 -0700, Randal L. Schwartz <merlyn@stonehenge.com> wrote: > > >>>>> ""Ankur" == "Ankur Gupta" <[EMAIL PROTECTED]> writes: > > "Ankur> Sonika Sachdeva <mailto:[EMAIL PROTECTED]> scribbled on > Tuesday, > "Ankur> April 11, 2006 4:27 PM: > > >> Actually I am looking for AND and OR combinations..and the number of > >> words are not fixed. > >> ie @words is the variable and I need to have OR and AND combinations > >> of @words. to match in LINES How do I go about it? > > "Ankur> perldoc -f eval > > or, instead of the dangerous code that this might result in, as in: > > "Ankur> my $string = join " $condition ", map { "/$_/" } @words; > > Ick ick... my eyes hurt looking at that! > > You could instead keep in mind that: > > /^(?=.*foo)(?=.*bar)/ > > is true only when the string contains *both* foo and bar. > So, to turn a list @words into an and'ed regex, it's merely: > > my $regex = "^" . join "", map "(?=.*\Q$_\E)", @words; > $regex = qr/$regex/; # compile it > > There's your "and". Safe for all inputs. (Note the \Q). > > -- > Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 > 0095 > <merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/> > Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. > See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl > training! > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > <http://learn.perl.org/> <http://learn.perl.org/first-response> > > >