in reply to using && with regex in an if() statement

What do you actually have in $sentence? I note that you're using /g on both matches, so the 2nd will start searching where the first left off, rather than at the beginning. Your "fixed" version circumvents that behavior (and makes me wonder what you hope to accomplish with the /g modifier). This works as anyone would expect it to:
my $sentence = 'I got a foo and a bar in me'; if ($sentence =~ /foo/gis && $sentence =~ /bar/gis) { print "I'm doing something with $sentence\n"; }
whereas reversing bar and foo in the sentence cause the test to fail.

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: using && with regex in an if() statement
by Tachoknight (Initiate) on Mar 14, 2006 at 22:20 UTC
    Yes, the problem was the /g....I removed it and all was well.

    I am trying to create a "natural language" parser, and a "real" sentence would have been something like

    Fred bought the car
    or
    The car was bought by Fred

    So I was searching for car and bought, but I thought /g would find the words globally through the text, regardless of position.

    I apologize: I'm a real newbie with regular expressions; most of my Perl work has not involved any real searches to date, but that has now changed and I'm going through *that* chapter that I always kinda meant to get back to someday to figure out....

    Thanks again!

    Tacho