如何在MySQL 8.0.5+中将REGEXP与正则表达式转义(如单词边界)一起使用? [英] How to use REGEXP with regex escapes like word boundary in MySQL 8.0.5+?
问题描述
在使用ICU-REGEXP在单词边界上执行搜索的MySQL(8.0.5+)中,它不起作用. 据我了解,应该是
It does not work in MySQL (8.0.5+) using ICU-REGEXP to perform a search on the word boundary. As far as I understand it should be a-la
$ mysql -e 'SELECT REGEXP_LIKE("aaa abc ccc", ".*\b+abc\b+.*")'
+---------------------------------------------+
| REGEXP_LIKE("aaa abc ccc", ".*\b+abc\b+.*") |
+---------------------------------------------+
| 0 |
+---------------------------------------------+
但是此选项不起作用.
推荐答案
首先,请注意REGEXP_REPLACE
可以部分匹配字符串 ,并且在搜索词之前和之后都不需要.*
.
First, note that REGEXP_REPLACE
can match strings partially, and you do not need .*
before and after a search word.
\
字符应转义以定义文字反斜杠,因为\
本身允许转义MySQL引擎的字符.参见 MySQL 8文档:
The \
char should be escaped in order to define a literal backslash, since \
itself allows escaping characters for the MySQL engine. See this MySQL 8 documentation:
注意
由于MySQL在字符串中使用C转义语法(例如,\n
表示换行符),因此必须将在 expr 中使用的所有\
都加倍.和 pat 参数.
Note
Because MySQL uses the C escape syntax in strings (for example,\n
to represent the newline character), you must double any\
that you use in your expr and pat arguments.
因此,您需要
REGEXP_LIKE("aaa abc ccc", "\\babc\\b")
这篇关于如何在MySQL 8.0.5+中将REGEXP与正则表达式转义(如单词边界)一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!