Sara wrote: > I am at a loss here to generate REGEX for my problem. > > I have an input query coming to my cgi script, containg a word (with or > without spaces e.g. "blood" "Globin Test" etc). > What I am trying to do is to split this word (maximum of 3 characters) and > find the BEST possible matching words within mySQL database. For example if > the word is "blood" > > I want to get results using regex: > > for "blood": &check(blo) then &check(loo) &check(ood) > for "Globin Test": &check(Glo) then &check(lob) &check(obi) &check(bin) > &check(Tes) &check(est) > > TIA. >
Sounds like you need a "split" then a "substr" rather than a regex, though I suppose it would work if you really wanted one, I wouldn't. perldoc -f split perldoc -f substr It will also be faster to combine everything into one select rather than for each possible "token", but at the least if you are going to do multiple selects use 'prepare' with placeholders and only prepare the query once. So, -- UNTESTED -- my @tokens = split ' ', $entry; my @words; foreach my $token (@tokens) { push @words, substr $token, 0, 3; push @words, substr $token, -3, 3; } (or you can put the following into the above foreach however you would like) my $where = ''; my @bind; foreach my $word (@words) { $where .= ' OR ' if $where ne ''; $where .= "(def LIKE ?)"; push @bind, "%$word%"; } my $sth = $dbh->prepare("SELECT * FROM medical WHERE $where"); $sth->execute(@bind); while (my @row = $sth->fetchrow_array) { print join ' ', @row; print "\n"; } This also prevents SQL injection by quoting the query words properly. > Sara. > http://danconia.org > sub check { > my $check = $dbh -> prepare("SELECT * FROM medical WHERE def LIKE '%$query%' > "); > $check->execute(); > while (my @row = $check -> fetchrow_array()) { > print "blah blah blah\n"; > } > } > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>