Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL REGEXP中的负反向引用_Mysql_Regex_Backreference - Fatal编程技术网

MySQL REGEXP中的负反向引用

MySQL REGEXP中的负反向引用,mysql,regex,backreference,Mysql,Regex,Backreference,MySQL手册并没有详细说明它支持什么表达式,所以我不确定MySQL是否可以实现以下功能 我正在尝试使用RLIKE创建一个查询,它与以下内容匹配 任务是从SQL中获取至少包含给定句子中任意两个单词的所有句子 比方说,我在正则表达式中使用了一些特定的词: hello, dog 我在数据库中有以下句子: hello from dog hello hello cat dog says hello dog dog goes away big bad dog 从这些我只想匹配 hello from d

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)'
RLIKE '(hello OR dog) anything can be here (hello OR dog, but not the word which already was in the previous group)'
问题是——我也得到了那些不需要的东西

hello hello cat
dog dog goes away
所以我想,我需要在第二个(hello | dog)之前有一个反向参考

在伪代码中,它将如下所示:

SELECT *
FROM test
WHERE 
test RLIKE '(hello|dog).*(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++代码生成,所以它不应该太复杂,不能生成。

(),因此根本不支持反向引用。它也不支持lookaround,您需要它来构造一个可以处理这个问题的正则表达式

因此,您必须详细说明所有可能的组合:

hello.*dog|dog.*hello

当然,如果候选匹配项的数量增加,这将变得很麻烦,因此正则表达式在MySQL中不是合适的工具,除非您可以安装/使用正则表达式

这听起来更像是为(然后简单地查询索引)或
(hello.*dog | dog.*hello)
?(hello.*dog.*dog.*hello)构建单词->句子映射索引更有意义,如果总是只有两个单词,那就更好了,但可能还有更多,然后我必须自己创造所有可能的单词组合。