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: Intermediate:
pattern matching

 



perlkid
stranger

May 4, 2000, 12:16 PM

Post #1 of 6 (737 views)
pattern matching Can't Post

 
Hello everyone,

I'm trying to get a search engine to match whole words but not the whole field. I want the engine to pick up jaguar when the field says jaguar cars, or jaguar parts, and just jaguar of course but this line of code picks up pieces of the words in the field that it's trying to compare the search word with. ex.

you type in seat, but the results are for seattle.

I go into the script and have it do exact matching.

type in seat, I get seat results.

type in jaguar I get jaguar results but it doesn't pick up the fields that say jaguar cars, or jaguar parts.

when I had it do exact matching I used;

$srchwrdlc = lc($in{'srchwrd'});
if ($wholestring eq "$srchwrdlc")
{
push (@results,$line);
}

then when it was picking up seat in seattle the code was

($one1, $two2, $three3, $four4, $five5, $six6, $seven7)=split(/ /, $words);

if ($wholestring =~/\b$one1/ && $wholestring =~/\b$two2/ && $wholestring =~/\b$three3/ && $wholestring =~/\b$four4/ && $wholestring =~/\b$five5/ && $wholestring=~/\b$six6/ && $wholestring =~/\b$seven7/){
push (@results,$line);}

$wholestring is the field I'm trying to match the searched word with,

$words is the search word.

How can I get the engine to pick up jaguar car results and jaguar results when you type in jaguar but get seat resutls when I type in seat, not seattle results.

Whole word matching but no whole field?

Thanks a lot,

perlkid


Peter Van Hoecke
Deleted

May 5, 2000, 1:51 AM

Post #2 of 6 (737 views)
Re: pattern matching [In reply to] Can't Post

I don't know if I understood the question correctly, but it seems to me that you have to use /(^| )$one1( |$)/ in the big if statement (same trick for $two2,...).
This states that the pattern has to be preceded by either the beginning of the line or a whitespace, and that the pattern has to be followed by a whitespace or the end of the line. If you want to, you can add any character between the brackets, if it's preceded by |

If this wasn't what you were looking for, that I apologize...

Peter.Van_Hoecke@ecom.be

PS: this would have been a valid entry for the new forum Wink

[This message has been edited by Peter Van Hoecke (edited 05-05-2000).]


perlkid
stranger

May 6, 2000, 1:32 AM

Post #3 of 6 (737 views)
Re: pattern matching [In reply to] Can't Post

  
Thank Youuuu Peter Van Hoecke!

perlkid


turkscripts
Deleted

May 7, 2000, 7:07 PM

Post #4 of 6 (737 views)
Re: pattern matching [In reply to] Can't Post

A better way might be:
/(^|\b)$one1(\b|$)/

or,

/(^|\s)$one1(\s|$)/

\b catches all \W to \w transitions. For example:
jaguar's will match if you use \b, but won't match if you use \s as word separators.


perlkid
stranger

May 8, 2000, 12:42 AM

Post #5 of 6 (737 views)
Re: pattern matching [In reply to] Can't Post

 
Ok, I'll try that code too. Thanks again turkscripts.

perlkid


Peter Van Hoecke
Deleted

May 8, 2000, 2:53 AM

Post #6 of 6 (737 views)
Re: pattern matching [In reply to] Can't Post

Whoops, didn't think of that... my bad!

good call turkscripts!

 
 


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

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