gpt4 book ai didi

MySQL Regex - 如何查找特定的连续单词集

转载 作者:行者123 更新时间:2023-11-29 19:18:26 25 4
gpt4 key购买 nike

我正在尝试使用此正则表达式字符串查询我的 MySQL 数据库,

^(?=.*?\\bAuto\\b)(?=.*?\\bAnd\\b)(?=.*?\\bFashion\\b)(?=.*?\\bFair\\b).*$

在我的本地主机上它工作正常,但在远程服务器上我收到如下错误:

[03-Mar-2017 09:49:47 UTC] PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1139 Got error 'repetition-operator operand invalid' from regexp' in /home/public_html/assets/sys/pdo.php:491

仅供引用,生成的查询字符串如下:

$SELECT * FROM table WHERE name REGEXP "^(?=.*?\\bAuto\\b)(?=.*?\\bAnd\\b)(?=.*?\\bFashion\\b)(?=.*?\\bFair\\b).*$" AND status = 1

最佳答案

MySQL regex基于 POSIX,并且不支持您的正则表达式模式中的以下内容:

  • (?=...) - 环视(既不是前瞻,也不是后顾)
  • *? - 惰性量词
  • \\b - 字边界写为[[:<:]] (单词位置的开头)和 [[:>:]] (单词位置的结尾)。

作为^(?=.*?\\bAuto\\b)(?=.*?\\bAnd\\b)(?=.*?\\bFashion\\b)(?=.*?\\bFair\\b).*$正则表达式匹配包含正向先行中列出的所有完整单词的任何字符串,您只能将当前的正则表达式重写为一系列 WHERE name REGEXP "<whole_word>" :

WHERE name REGEXP "[[:<:]]Auto[[:>:]]" AND WHERE name REGEXP "[[:<:]]And[[:>:]]" AND WHERE name REGEXP "[[:<:]]Fashion[[:>:]]" AND WHERE name REGEXP "[[:<:]]Fair[[:>:]]"

此外,如果您需要区分大小写的搜索,则需要使用BINARY之后REGEXP .

关于MySQL Regex - 如何查找特定的连续单词集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42576117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com