Home > Archive > PERL CGI Beginners > August 2005 > Regex Problem.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| 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.
Sara.
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";
}
}
| |
| Greg Jetter 2005-08-18, 5:55 pm |
| On Thursday August 18 2005 11:48 am, 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.
>
> Sara.
>
> 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";
> }
> }
try using RLIKE instead of LIKE
WHERE def RLIKE '$query'
Greg
| |
| Wiggins d'Anconia 2005-08-18, 5:55 pm |
| 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";
> }
> }
>
| |
|
| That's worked like a charm, You ALL are great.
Thanks everyone for help.
Sara.
----- Original Message -----
From: <webmaster@echtwahr.com>
To: "'Sara'" <sara.samsara@gmail.com>
Sent: Thursday, August 18, 2005 10:50 PM
Subject: RE: Regex Problem.
> Hi Sara,
>
> what is about somthing like
> $string = 'blood';
> for($i=0; $i<=length($string)-3;$i++) {
>
> check(substr($string,$i,3));
> }
>
>
>
> Mit freundlichen Grüssen
> Ihr echtwahr.Webmaster
>
> ------------------------
> http://www.echtwahr.de
> http://www.echtwahr.com
>
>
>
|
|
|
|
|