in reply to DBD-SQLite Regexp
That way, you get your literal whitespace on either side of your tag. This suffers from limitation that it will miss for leading or trailing words and is unlikely to behave the way you intend around punctuation. You could do better with:SELECT ID, sentence FROM texts WHERE sentence LIKE '% $query %'
But this still doesn't do a great job. If I were to do this, I would just do the simple querySELECT ID, sentence FROM texts WHERE sentence LIKE '% $query %' OR sentence LIKE '$query %' OR sentence LIKE '% $query' OR sentence = '$query'
and then filter the results against an appropriate regular expression, like /\b\Q$query\E\b/.SELECT ID, sentence FROM texts WHERE sentence LIKE '%$query%'
As a side note, you should consider using Placeholders_and_Bind_Values (see DBI) for database access. It protects you from all sorts of security exploits and handles messy escaping issues no trouble. I would actually do the above as
my $sth = $dbh->prepare(<<'EOSQL'); SELECT ID, sentence FROM texts WHERE sentence LIKE ? ESCAPE ? EOSQL $sth->execute("%$query%", '\\'); my $results = []; while (my @row = $sth->fetchrow_array) { push @$results, \@row if $row[1] =~ /\b\Q$query\E\b/; }
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
|
|---|