我正在为我的网站编写自定义搜索引擎。我正在尝试使用 MySQL REGEXP 功能。我希望能够搜索由空格分隔的单词,以避免在单词上获得后缀或前缀的机会。例如,我正在尝试搜索“appreciate”,我想要 appreciate、not appreciated 或 unappreciate 或 unappreciated。关于如何使用 MySQL 的 REGEXP 执行此操作的任何想法?我的想法是寻找像这样的空间:
^appreciate$|^appreciate[:space:]|[:space:]appreciate$|[:space:]appreciate[:space:]
我确信他们是一种更好的方法,但我不知道这是否有效
最佳答案
我想你想要的是这样的:
SELECT 'I appreciate you' REGEXP '[[:<:]]appreciate[[:>:]]'; /* matches */
[[<:]]
和 [[>:]]
是单词边界。来自 the manual :
These markers stand for word boundaries. They match the beginning and end of words, respectively. A word is a sequence of word characters that is not preceded by or followed by word characters. A word character is an alphanumeric character in the alnum class or an underscore (_).
编辑:澄清一下,这也处理单词后有换行符或逗号等的情况
关于MySQL 正则表达式搜索查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8577481/