代码之家  ›  专栏  ›  技术社区  ›  JustAMartin

MySQL REGEXP中的负反向引用

  •  3
  • JustAMartin  · 技术社区  · 14 年前

    MySQL手册并没有详细说明它支持哪些表达式,所以我不确定MySQL是否可以实现以下功能。

    任务是从SQL中获取给定句子中至少包含任意两个单词的所有句子。

    比方说,我在regex中有一些特定的词要使用:

    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
    

    所以我想,我需要一个第二个之前的回溯(你好|狗)。

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

    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 regex中完成吗? 或者也许有更好的方法来编写同样的事情,也考虑到这个查询会被一些C++代码所生成,所以它不应该太复杂了吗?

    1 回复  |  直到 14 年前
        1
  •  5
  •   Tim Pietzcker    14 年前

    MySQL uses a Posix Extended Regular Expression engine ( POSIX ERE

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

    hello.*dog|dog.*hello
    

    LIB_MYSQLUDF_PREG .