Tachoknight has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, monks. I have come in search of wisdom that I have not found anywhere else; neither the the Camel book nor Googling has come up with an answer to my question and I hope that it may be answered here. I am trying to find multiple words in a sentence, and if I find them, act accordingly. To wit, I wrote:
if ($sentence =~ /foo/gis && $sentence =~ /bar/gis) { # do something further with $sentence }
But it is wrong, the second expression is always false, even if the sentence does contain a "bar". I rewrote it to say:
if ($sentence =~ /foo/gis) { if ($sentence =~ /bar/gis) { # now do something with the sentence } }
but that doesn't work either! The only way I could get it to work was:
if ($sentence =~ /foo/gis) { my $final_test = $sentence; if ($final_test =~ /bar/gis) { # *now* do something with the sentence... } }
I apologize if this is a real "duh" question; it seemed like it would make sense to me (a C++ programmer) but the second try really confused me as I figured it surely couldn't fail, it was just doing another search. I beg wisdom and mercy for my cluelessness. Thank you, Tacho

Replies are listed 'Best First'.
Re: using && with regex in an if() statement
by Roy Johnson (Monsignor) on Mar 14, 2006 at 17:02 UTC
    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.
      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
Re: using && with regex in an if() statement
by kvale (Monsignor) on Mar 14, 2006 at 17:00 UTC
    The snippet
    my $sentence = "foo bar"; if ($sentence =~ /foo/gis && $sentence =~ /bar/gis) { print "Got here\n"; }
    prints "Got here", so perhaps check your sentence again.

    -Mark

Re: using && with regex in an if() statement
by QM (Parson) on Mar 14, 2006 at 17:04 UTC
    Update: I think Roy Johnson has it right -- watch out for the /g flag.

    Something else is wrong:

    >perl -demo Loading DB routines from perl5db.pl version 1.28 Editor support available. Enter h or `h h' for help, or `perldoc perldebug' for more help. main::(-e:1): mo DB<1> $sentence = 'sdfsdfsdf foo asdfklj bar ;alkdjdf' DB<2> x $sentence 0 'sdfsdfsdf foo asdfklj bar ;alkdjdf' DB<3> p "match" if $sentence =~ /foo/ && $sentence =~ /bar/ match
    Either $sentence doesn't contain what you think it does, or you haven't run the code you posted. Or you need the /m flag. Or something else.

    Please post the actual code (or better, a minimal example that behaves the same way), including the value of the variable involved in the match.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

A reply falls below the community's threshold of quality. You may see it by logging in.