RegEx and AND

T

tinu

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
 
P

Paul Lalli

tinu said:
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
 
X

Xicheng Jia

tinu said:
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
 
A

anno4000

Xicheng Jia said:
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
 
B

Brian Wakem

tinu said:
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) ) {
...
}
 
X

Xicheng Jia

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
 
A

anno4000

Xicheng Jia said:
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
 
A

anno4000

Brian Wakem said:
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
 
B

Big and Blue

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/;
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top