CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to match words in a string/sentence??

 



Kougar
stranger

Jun 14, 2001, 1:52 AM

Post #1 of 6 (492 views)
How to match words in a string/sentence?? Can't Post

Hi.. Could anyone kindly help pls?? I've a string and I'm doing some checking to see that if this string contains certain words, then e.g. print "Hi there". Else print"Oh no..."



mhx
Enthusiast / Moderator

Jun 14, 2001, 3:07 AM

Post #2 of 6 (488 views)
Re: How to match words in a string/sentence?? [In reply to] Can't Post

Hi!

Your question isn't really precise, but I'll try answering anyway. Let's say you've got a $string and want to check if the words 'foo' and 'bar' occur anywhere in the string:

Code
my $string = 'This is not a bar, you fool!'; 

if( $string =~ /foo/ and $string =~ /bar/ ) {
print "Hi there\n";
}
else {
print "Oh no...\n";
}

This will print 'Hi there' because of bar and fool. There are several other options you have, but you've got to be a bit more precise then...

Hope this helps.

-- Marcus



Kougar
stranger

Jun 14, 2001, 5:41 PM

Post #3 of 6 (475 views)
Re: How to match words in a string/sentence?? [In reply to] Can't Post

Hey Marcus...

Thanks a lot for your help... Appreciates it.



Cure
User

Jun 15, 2001, 7:39 PM

Post #4 of 6 (460 views)
Re: How to match words in a string/sentence?? [In reply to] Can't Post

using a regex for a simple pattern like that is a regex OVERKILL....

use the index(), it's made for simple patterns like that and it's much faster

my $string = 'This is not a bar, you fool!';

if( index($string,'foo') != -1 and index($string,'bar') != -1 )

{ print "Hi there\n"}

else { print "Oh no...\n"}



Cure



mhx
Enthusiast / Moderator

Jun 17, 2001, 2:31 PM

Post #5 of 6 (451 views)
Re: How to match words in a string/sentence?? [In reply to] Can't Post

Hi cure,

If you'd like to call a CPU usage increased by 28% an overkill for such a tiny thing, I agree. I would have been surprised if the regex would need so much longer. To the best of my knowledge, this regex does nearly the same internally as the index() functions does, except for compilation. Anyway, I've benchmarked our two code snippets:

Code
Benchmark: timing 2000000 iterations of cure, mhx... 
cure: 21 wallclock secs (21.04 usr + 0.00 sys = 21.04 CPU) @ 95057.03/s (n=2000000)
mhx: 27 wallclock secs (26.97 usr + 0.00 sys = 26.97 CPU) @ 74156.47/s (n=2000000)

My regexes can run 74000 times per second, while your index() can do 95000 runs. Your solution is definetely faster. But, if you run the code with use re 'debug' you will notice that the compiled regex searches for an exact three character match 'bar' / 'foo' and nothing else:

Code
Compiling REx `foo' 
size 3 first at 1
1: EXACT <foo>(3)
3: END(0)
anchored `foo' at 0 (checking anchored isall) minlen 3
Compiling REx `bar'
size 3 first at 1
1: EXACT <bar>(3)
3: END(0)
anchored `bar' at 0 (checking anchored isall) minlen 3

There's of course some overhead because this matching is embedded in the regex engine. But Perl's regex engine is quite fast.
If the code would be used in a loop repeating several thousand times or would be embedded in a big project, I would absolutely favour your solution. But in a script as tiny as the one we're discussing here, I think the regex solution is more readable and more intuitive (at least to me Wink).
I don't want to criticize here, your solution is right and faster than mine (and I'm sure I'll remember it and use it if appropriate). I just wanted to point out that using the regex here is not as bad as you said. Using .* is more of an overkill in most of its applications...

-- Marcus



Kougar
stranger

Jun 17, 2001, 9:58 PM

Post #6 of 6 (447 views)
Re: How to match words in a string/sentence?? [In reply to] Can't Post

Dear Cure...

Thanks anyway... U and Marcs provided great help to me and definitely appreciates u guys help....

BIG THANK YOU TO UUUUUU..........


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives