Reputation: 28722
I have the following query:
SELECT * `library` WHERE
(
UCASE( `text` ) REGEXP '((^KEYWORD|[[.space.]]KEYWORD)([[.space.]]|[[.comma.]]|[[.period.]]|[[.colon.]]|[[.semicolon.]]|[[.apostrophe.]]|[[.quotation-mark.]]|[[.exclamation-mark.]]|[[.question-mark.]]))((^KEYWORD2|[[.space.]]KEYWORD2)([[.space.]]|[[.comma.]]|[[.period.]]|[[.colon.]]|[[.semicolon.]]|[[.apostrophe.]]|[[.quotation-mark.]]|[[.exclamation-mark.]]|[[.question-mark.]]))'
)
** EDIT **
added an sql fiddle:
http://sqlfiddle.com/#!2/8f822/2/0
In readable format this is:
SELECT * FROM `library` WHERE
(
UCASE( `text` ) REGEXP '
(
(
^KEYWORD | [[.space.]]KEYWORD
)
(
[[.space.]] |
[[.comma.]] |
[[.period.]] |
[[.colon.]] |
[[.semicolon.]] |
[[.apostrophe.]] |
[[.quotation-mark.]] |
[[.exclamation-mark.]] |
[[.question-mark.]]
)
)
(
(
^KEYWORD2 | [[.space.]]KEYWORD2
)
(
[[.space.]] |
[[.comma.]] |
[[.period.]] |
[[.colon.]] |
[[.semicolon.]] |
[[.apostrophe.]] |
[[.quotation-mark.]] |
[[.exclamation-mark.]] |
[[.question-mark.]]
)
)
)'
Now I need the query to do the following thing, and I'm hitting a brick wall here.
If keyword appears in text, starting the text or preceded by a space, followed by one of the special chars, then show the result.
This doesn't really work like I need it to, but I can't find in the documentation how to make a match if both appear. e.g. trying to match the string "keyword,"
,"keyword "
or "keyword;"
Then I need to match a same type of query for keyword 2.
Then both expressions for keyword and keyword2 need to be true and I only need those results.
For example the texts
A lazy brown fox jumped over the fence.
Then he found a chicken.
Then he ate the chicken and ran from the farmer.
and
A fox found a chicken.
Then he ate the chicken and ran from the farmer.
Then fox jumped over the fence.
Should come op with he following keywords fence
chicken
but this shouldn't match the following text
A lazy brown fox jumped over the fences.
Then he found a chicken.
Then he ate the chicken and ran from the farmer.
ANSWER
The final query has become
SELECT *
FROM`library`
WHERE
UCASE(`text` ) REGEXP '(^| )FENCE[[.space.][.comma.][.period.][.colon.][.semicolon.][.apostrophe.][.quotation-mark.][.exclamation-mark.][.question-mark.]]'
AND
UCASE(`text` ) REGEXP '(^| )CHICKEN[[.space.][.comma.][.period.][.colon.][.semicolon.][.apostrophe.][.quotation-mark.][.exclamation-mark.][.question-mark.]]'
because regex doesn't support random order searches, and since I don't wish to do n to the power of number of keywords search strings this is the best possible solution.
Thanks all for watching.
Upvotes: 1
Views: 691
Reputation: 4954
You could use the regex (^| )KEYWORD[- ,.:;'"!?]
and put two REGEXP tests in your WHERE clause ANDed together. The first part (^| )
is a regex which allows us to match the start of the text or a space. Next comes the keyword which has to be matched as is and then a character class between []
for which a single character from the class has to match. It is like an OR for single characters instead of |
for complete regexes.
SELECT *
FROM`library`
WHERE
UCASE(`text` ) REGEXP '(^| )KEYWORD[- ,.:;''"!?]'
AND
UCASE(`text` ) REGEXP '(^| )KEYWORD2[- ,.:;''"!?]'
Disclaimer : I have not tested the SQL statement and I can't remember if the apostrophe has to be doubled inside the string.
Upvotes: 1