MySQL 手册对它支持的表达式不是很详细,所以我不确定 MySQL 是否可以实现以下操作。
我正在尝试使用与以下内容匹配的 RLIKE 创建一个查询。
任务是从 SQL 中获取至少包含给定句子中任意两个单词的所有句子。
比方说,我在正则表达式中有一些特定的词要使用:
hello, dog
我在数据库中有以下句子:
hello from dog
hello hello cat
dog says hello
dog dog goes away
big bad dog
从那些我只想匹配的
hello from dog
dog says hello
现在我有这样的:
SELECT *
FROM test
WHERE
test RLIKE '(hello|dog).*(hello|dog)'
问题是 - 我也得到了那些不需要的
hello hello cat
dog dog goes away
所以我想,我需要在第二个 (hello|dog) 之前进行反向引用。
在伪代码中,它看起来像这样:
RLIKE '(hello OR dog) anything can be here (hello OR dog, but not the word which already was in the previous group)'
所以它可能是这样的:
'(hello|dog).*(negative backreference to the 1st group goes here)(hello|dog)'
可以在 MySQL 正则表达式中完成这种负反向引用吗?或者也许有一些更好的方法来编写做同样事情的正则表达式,但也考虑到查询将由一些 C++ 代码生成,所以生成它不应该太复杂?