Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > RegEx and AND

Reply
Thread Tools

RegEx and AND

 
 
tinu
Guest
Posts: n/a
 
      07-13-2006
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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      07-13-2006
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

 
Reply With Quote
 
 
 
 
Xicheng Jia
Guest
Posts: n/a
 
      07-13-2006
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

 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      07-13-2006
Xicheng Jia <(E-Mail Removed)> 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
 
Reply With Quote
 
Brian Wakem
Guest
Posts: n/a
 
      07-13-2006
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
 
Reply With Quote
 
Xicheng Jia
Guest
Posts: n/a
 
      07-13-2006
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de wrote:
> Xicheng Jia <(E-Mail Removed)> 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

 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      07-13-2006
Xicheng Jia <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> (E-Mail Removed)-berlin.de wrote:
> > Xicheng Jia <(E-Mail Removed)> 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
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      07-14-2006
Brian Wakem <(E-Mail Removed)> 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
 
Reply With Quote
 
Big and Blue
Guest
Posts: n/a
 
      07-14-2006
(E-Mail Removed)-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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex testing and UTF8 awarenes or Regex and numeric pattern matching sln@netherlands.com Perl Misc 2 03-10-2009 03:51 AM
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Python 8 09-26-2006 03:24 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Perl Misc 2 09-25-2006 03:15 AM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM



Advertisments