RegEx and AND

Discussion in 'Perl Misc' started by tinu, Jul 13, 2006.

  1. tinu

    tinu Guest

    Hi All

    I need to do a simple RegEx expression for and AND. I dont know how to
    do this.
    I have the following string:
    "The tree is taller then the flower".

    I would like to search for "tree" AND "flower". How Do I do this?

    Thanks for your help
     
    tinu, Jul 13, 2006
    #1
    1. Advertising

  2. tinu

    Paul Lalli Guest

    tinu wrote:
    > I need to do a simple RegEx expression for and AND. I dont know how to
    > do this.
    > I have the following string:
    > "The tree is taller then the flower".
    >
    > I would like to search for "tree" AND "flower". How Do I do this?


    Why are you assuming you need one regexp? Why can't you do:

    my $line = "The tree is taller than the flower."
    if ($line =~ /tree/ && $line =~ /flower/) { ... }

    I suppose if you really wanted one regexp, you could so something
    bizarre like:
    if ($line =~ /tree.*flower|flower.*tree/) { ... }
    to account for the possibility that the words are in either order. . .

    Paul Lalli
     
    Paul Lalli, Jul 13, 2006
    #2
    1. Advertising

  3. tinu

    Xicheng Jia Guest

    tinu wrote:
    > Hi All
    >
    > I need to do a simple RegEx expression for and AND. I dont know how to
    > do this.
    > I have the following string:
    > "The tree is taller then the flower".
    >
    > I would like to search for "tree" AND "flower". How Do I do this?
    >
    > Thanks for your help


    if ($line =~ /^(?=.*tree)(?=.*flower)/) { ... }

    Xicheng
     
    Xicheng Jia, Jul 13, 2006
    #3
  4. tinu

    -berlin.de Guest

    Xicheng Jia <> wrote in comp.lang.perl.misc:
    > tinu wrote:
    > > Hi All
    > >
    > > I need to do a simple RegEx expression for and AND. I dont know how to
    > > do this.
    > > I have the following string:
    > > "The tree is taller then the flower".
    > >
    > > I would like to search for "tree" AND "flower". How Do I do this?
    > >
    > > Thanks for your help

    >
    > if ($line =~ /^(?=.*tree)(?=.*flower)/) { ... }


    Why do you anchor the regex? It doesn't change what it matches.

    This is the one-regex solution the OP asked for, but in a practical
    program I'd always prefer two and-connected matches.

    $line =~ /tree/ and $line =~ /flower/;

    has been suggested. It is much easier to read and substantially
    faster too.

    Anno
     
    -berlin.de, Jul 13, 2006
    #4
  5. tinu

    Brian Wakem Guest

    tinu wrote:

    > Hi All
    >
    > I need to do a simple RegEx expression for and AND. I dont know how to
    > do this.
    > I have the following string:
    > "The tree is taller then the flower".
    >
    > I would like to search for "tree" AND "flower". How Do I do this?
    >
    > Thanks for your help



    No need to use a regex. index is much more suited and faster.


    if ( (index($string,'tree') != -1) and (index($string,'flower') != -1) ) {
    ...
    }


    --
    Brian Wakem
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
     
    Brian Wakem, Jul 13, 2006
    #5
  6. tinu

    Xicheng Jia Guest

    -berlin.de wrote:
    > Xicheng Jia <> wrote in comp.lang.perl.misc:
    > > tinu wrote:
    > > > Hi All
    > > >
    > > > I need to do a simple RegEx expression for and AND. I dont know how to
    > > > do this.
    > > > I have the following string:
    > > > "The tree is taller then the flower".
    > > >
    > > > I would like to search for "tree" AND "flower". How Do I do this?
    > > >
    > > > Thanks for your help

    > >
    > > if ($line =~ /^(?=.*tree)(?=.*flower)/) { ... }

    >
    > Why do you anchor the regex? It doesn't change what it matches.
    >
    > This is the one-regex solution the OP asked for, but in a practical
    > program I'd always prefer two and-connected matches.
    >
    > $line =~ /tree/ and $line =~ /flower/;
    >
    > has been suggested. It is much easier to read and substantially
    > faster too.


    There are some minor differences, please check the book "Computer
    Science & Perl Programming: Best of TPJ", which has one chapter to
    compare the cons and pros of all proposed methods in this thread.

    Xicheng
     
    Xicheng Jia, Jul 13, 2006
    #6
  7. tinu

    -berlin.de Guest

    Xicheng Jia <> wrote in comp.lang.perl.misc:
    > -berlin.de wrote:
    > > Xicheng Jia <> wrote in comp.lang.perl.misc:
    > > > tinu wrote:
    > > > > Hi All
    > > > >
    > > > > I need to do a simple RegEx expression for and AND. I dont know how to
    > > > > do this.
    > > > > I have the following string:
    > > > > "The tree is taller then the flower".
    > > > >
    > > > > I would like to search for "tree" AND "flower". How Do I do this?
    > > > >
    > > > > Thanks for your help
    > > >
    > > > if ($line =~ /^(?=.*tree)(?=.*flower)/) { ... }

    > >
    > > Why do you anchor the regex? It doesn't change what it matches.
    > >
    > > This is the one-regex solution the OP asked for, but in a practical
    > > program I'd always prefer two and-connected matches.
    > >
    > > $line =~ /tree/ and $line =~ /flower/;
    > >
    > > has been suggested. It is much easier to read and substantially
    > > faster too.

    >
    > There are some minor differences, please check the book "Computer
    > Science & Perl Programming: Best of TPJ", which has one chapter to
    > compare the cons and pros of all proposed methods in this thread.


    Oh yeah, Jeffry Friedell at his compulsive best. I still maintain
    that the two-regex solution is the standard one.

    Anno
     
    -berlin.de, Jul 13, 2006
    #7
  8. tinu

    -berlin.de Guest

    Brian Wakem <> wrote in comp.lang.perl.misc:
    > tinu wrote:
    >
    > > Hi All
    > >
    > > I need to do a simple RegEx expression for and AND. I dont know how to
    > > do this.
    > > I have the following string:
    > > "The tree is taller then the flower".
    > >
    > > I would like to search for "tree" AND "flower". How Do I do this?
    > >
    > > Thanks for your help

    >
    >
    > No need to use a regex. index is much more suited and faster.
    >
    >
    > if ( (index($string,'tree') != -1) and (index($string,'flower') != -1) ) {
    > ..
    > }


    It is a good engineering principle to use the simplest tool that does the
    job, but other criteria are more important. In this case, readability
    takes precedence. The Perl programmer's way of saying "$string contains
    'tree' somewhere" is

    $string =~ /tree/;

    "index($string,'tree') != -1" is not a very intuitive way of saying
    the same thing. Boolean "1 + index(...)" is only slightly better.

    Speed should only be a secondary concern before a program is up and
    running, but the speed difference in index() and m// is marginal
    anyway. Indeed, on my machine the regex solution is slightly faster.

    Anno
     
    -berlin.de, Jul 14, 2006
    #8
  9. tinu

    Big and Blue Guest

    -berlin.de wrote:

    > The Perl programmer's way of saying "$string contains
    > 'tree' somewhere" is
    >
    > $string =~ /tree/;


    However, if you are looking for the *word* "tree" (rather than, say,
    "streetwise") you'll need:

    $string =~ /\btree\b/;



    --
    Just because I've written it doesn't mean that
    either you or I have to believe it.
     
    Big and Blue, Jul 14, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee
    Replies:
    1
    Views:
    972
    Ilias Lazaridis
    Sep 22, 2006
  2. Xah Lee
    Replies:
    8
    Views:
    488
    Ilias Lazaridis
    Sep 26, 2006
  3. Replies:
    3
    Views:
    834
    Reedick, Andrew
    Jul 1, 2008
  4. Xah Lee
    Replies:
    2
    Views:
    242
    Xah Lee
    Sep 25, 2006
  5. Replies:
    2
    Views:
    417
Loading...

Share This Page