MySQL 正则表达式搜索查询

标签 mysql regex

我正在为我的网站编写自定义搜索引擎。我正在尝试使用 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/

相关文章:

php - php将word设置为粗体的问题

mysql - 如何在1列中创建多个外键值?

python - 在 Python 中从数据库元组中获取项目

php - PHP创建表类别并正确获取url的问题

java - 如何在jtextbox上实时验证正则表达式?

sql - Oracle sql 终止符筛选(+奇怪的行为)

mysql - SQL 结合Where 和Select

mysql - 无法连接为使用 Ansible 创建的 MySQL 用户

regex - 使隐藏不禁用除 pdf 文件(包含在该文件夹中)之外的所有文件夹和 .tmp 文件

javascript - 需要 URL 的正则表达式