Need help with regular expression

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Need help with regular expression

Post by amir »

I am currently using on my php/Mysql db. The only problem is the expression is taking way to long to do the search of the db, even though the db is properly indexed. The expression finds an exact match in the Topic field which is what I wanted. However, the speed of search problem and the server load is a bigger problem. Here is the query with the regular expression.

Code: Select all

$query = "SELECT * FROM `View6` WHERE `Topic` REGEXP '[[:<:]]" .
 mysql_real_escape_string($SeeAlso) .
 "[[:>:]]' AND `Source` = $NNN OR (`Source` = $TTT) ORDER BY `Lnum` ASC";
Is there a way to change the regular expression above to find a white space before the

Code: Select all

$SeeAlso
variable if the

Code: Select all

$SeeAlso
variable is within a sentence or find the

Code: Select all

$SeeAlso
variable if the

Code: Select all

$SeeAlso
variable is the first word in the sentence. For example, if the user wants to find the word "fear" without the quotes, the regular expression would find...

fear
fearless
fearlessly
fearful
fearsome

...if these words appear at the beginning of a sentence or within a sentence.

Thank you in advance for any replies.
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

Just use the left side boundary, meaning (find all $SeeAlso that start with $SeeAlso or that $SeeAlso is after (non-word character, NOT -> [^a-zA-Z_0-9])

Code: Select all

$query = "SELECT * FROM View6 WHERE Topic REGEXP '[[:<:]]" . mysql_real_escape_string ( $SeeAlso ) . "' AND Source = " . $NNN . " OR Source = " . $TTT . " ORDER BY Lnum ASC";
printf
Post Reply