如何将 REGEXP 与转义序列(如字边界)一起使用?

How to use REGEXP with escape sequences like word boundary?

在 MySQL (8.0.5+) 中使用 ICU-REGEXP 对单词边界执行搜索不起作用。 据我了解应该是a-la

$ mysql -e 'SELECT REGEXP_LIKE("aaa abc ccc", ".*\b+abc\b+.*")'
+---------------------------------------------+
| REGEXP_LIKE("aaa abc ccc", ".*\b+abc\b+.*") |
+---------------------------------------------+
|                                           0 |
+---------------------------------------------+

但是这个选项不起作用。

首先,请注意 REGEXP_REPLACE 可以匹配字符串 部分 ,并且您不需要在搜索词前后 .*

应该转义 \ 字符以定义文字反斜杠,因为 \ 本身允许转义 MySQL 引擎的字符。看到这个 MySQL 8 documentation:

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")