MySQL REGEXP(参数顺序问题) [英] MySQL REGEXP (problem with params order)

查看:38
本文介绍了MySQL REGEXP(参数顺序问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查询:

SELECT * FROM (test) test_label WHERE REGEXP "AA(.*)BB" OR test_label REGEXP "BB(.*)AA"**

数据库(字符串):

<代码>1.AA BB 1002. AA BB 2003. BB AA 3004. BB CC 1005. AA CC 300

此查询返回行:1、2 和 3.

如何:

将此表达式合并为一个(但是 - 我希望它与一个 REGEXP 但不与 OR - 如果可能的话,它不得采用输入变量的顺序,我也不必搜索所有可能的组合)

现在,如果查询是AA BB",我必须找到所有可能的情况 - 在这种情况下,它只是:

  • AA(.)BB|BB(.)AA

但是如果查询是AA BB CC",那就是更多的情况了.

测试用例:

  1. 查询AA BB"返回第 1、2、3 行.
  2. 查询BB 300"仅返回第 3 行.
  3. 查询BB CC 100"仅返回第 4 行.
  4. 查询BB 100"返回第 1,4 行.

解决方案

AA(.*)BB|BB(.*)AA

应该可以,虽然我不明白你为什么要捕捉 AA 和 BB 之间的空间,所以 AA.*BB|BB.*AA 也能正常工作.>

如果可能的字符串数量增加,则取决于您希望允许的有效匹配项.如果你想要真正的"组合,我.e.AA, BB, CC 等列表的所有三个元素必须以任何顺序出现一次,然后 MySQL(使用 POSIX ERE 引擎,因此没有实现反向引用)让您别无选择,只能拼出所有可能的组合:

AA.*BB.*CC|AA.*CC.*BB|BB.*AA.*CC|BB.*CC.*AA|CC.*AA.*BB|CC.*BB.*AA

等等以获取更多元素.

Query:

SELECT * FROM (test) test_label WHERE REGEXP "AA(.*)BB" OR test_label REGEXP "BB(.*)AA"**

Database (string):

1. AA BB 100
2. AA BB 200
3. BB AA 300
4. BB CC 100
5. AA CC 300

This query returns rows: 1, 2 and 3.

How:

Combine this expression into one (but - i want it with one REGEXP but not with OR - if it is possible it must not take order of input vars and i don't must search for all possible combinations)

Now if query is "AA BB" i must find all possible cases - in this case it's only:

  • AA(.)BB|BB(.)AA

but if query is "AA BB CC" it is a lot more cases.

Test cases:

  1. query "AA BB" returns rows 1,2,3.
  2. query "BB 300" return only row 3.
  3. query "BB CC 100" return only row 4.
  4. query "BB 100" return rows 1,4.

解决方案

AA(.*)BB|BB(.*)AA

should do, although I don't understand why you would want to capture the space between AA and BB, so AA.*BB|BB.*AA would work just as well.

If the number of possible strings increases, it depends on what you want to allow as valid matches. If you want "true" combinations, i. e. all three elements of a list like AA, BB, CC must be present exactly once, in any order, then MySQL (using a POSIX ERE engine and therefore not having backreferences implemented) leaves you no choice but to spell out all possible combinations:

AA.*BB.*CC|AA.*CC.*BB|BB.*AA.*CC|BB.*CC.*AA|CC.*AA.*BB|CC.*BB.*AA

and so on for more elements.

这篇关于MySQL REGEXP(参数顺序问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆