【问题标题】:oracle regexp_like: using Perl regexp for AND operator equivalentoracle regexp_like:使用 Perl 正则表达式与运算符等效
【发布时间】:2020-06-12 23:53:16
【问题描述】:

在 Oracle REGEX_LIKE 中编写正则表达式时,是否有等效的 AND 运算符?我一直在尝试选择所有三个单词都必须包含在字符串中的情况。在下面的示例中,我想匹配 所有三个单词small & leather & goods 必须的所有实例被包含在字符串中。我尝试了许多在线正则表达式测试器,正则表达式语法正是我需要它做的,但是当我尝试在 REGEXP_LIKE 表达式中使用该语法时,我得到零匹配

regexp_like(GLOBAL_CATEGORY,'(?=.*small)(?=.*leather)(?=.*goods)^.*$','i') 

完成我正在寻找的正则表达式测试器示例: regexr.com/4vc1s

Men/Outerwear/Leather/Sale **(NO MATCH)**
Men > Accessories > Bags & Leather Goods > Small Leather Goods **(YES MATCH)**
Men > Accessories > Bags & Leather Goods > Bags **(NO MATCH)**
Men > Accessories > Bags & Leather Goods > Small Leather Goods **(YES MATCH)**
Men/Outerwear/Leather/Sale **(NO MATCH)**
Men/Small_Leather_Goods/Sale **(YES MATCH)**
Men/Outerwear/Leather **(NO MATCH)**
Men/Small_Leather_Goods **(YES MATCH)**
men>accessories>small>leather>goods **(YES MATCH)**

【问题讨论】:

  • 您的所有示例中的单词都按照您列出的顺序出现。如果最后一部分只是“小”,则该模式将匹配第二个示例。您能否确认您正在尝试以字符串中的任何顺序查找它们?
  • 是的,按照列出的顺序

标签: regex oracle regex-lookarounds regexp-like


【解决方案1】:

如果你不坚持使用正则表达式,那么简单的 instr 就可以了:

SQL> with test (col) as (
  2  select 'Men/Outerwear/Leather/Sale **(NO MATCH)**'                                      from dual union all
  3  select 'Men > Accessories > Bags & Leather Goods > Small Leather Goods **(YES MATCH)**' from dual union all
  4  select 'Men > Accessories > Bags & Leather Goods > Bags **(NO MATCH)**'                 from dual union all
  5  select 'Men > Accessories > Bags & Leather Goods > Small Leather Goods **(YES MATCH)**' from dual union all
  6  select 'Men/Outerwear/Leather/Sale **(NO MATCH)**'                                      from dual union all
  7  select 'Men/Small_Leather_Goods/Sale **(YES MATCH)**'                                   from dual union all
  8  select 'Men/Outerwear/Leather **(NO MATCH)**'                                           from dual union all
  9  select 'Men/Small_Leather_Goods **(YES MATCH)**'                                        from dual union all
 10  select 'men>accessories>small>leather>goods **(YES MATCH)**'                            from dual
 11  )
 12  select * from test
 13  where instr(lower(col), 'small')   > 0
 14    and instr(lower(col), 'leather') > 0
 15    and instr(lower(col), 'goods')   > 0;

COL
------------------------------------------------------------------------------
Men > Accessories > Bags & Leather Goods > Small Leather Goods **(YES MATCH)**
Men > Accessories > Bags & Leather Goods > Small Leather Goods **(YES MATCH)**
Men/Small_Leather_Goods/Sale **(YES MATCH)**
Men/Small_Leather_Goods **(YES MATCH)**
men>accessories>small>leather>goods **(YES MATCH)**

SQL>

(是的,我知道,(YES/NO MATCH) 不是字符串的一部分,但我懒得删除它。)

【讨论】:

  • 我认为这是最接近 OP 所展示的积极前瞻正则表达式(鉴于 Oracle 不支持前瞻断言)。
【解决方案2】:

假设您想要按顺序排列“小”、“皮革”和“商品”这几个词,请尝试:

regexp_like(GLOBAL_CATEGORY,'*small.*.leather.*goods*', 'i')

匹配任何字符
* 匹配前面子表达式的零次或多次出现
i 是一个匹配模式,它指定不区分大小写的匹配

Demo这里

【讨论】:

  • 以上工作。因为我现在知道 regexp_like 不遵循与 Perl 完全相同的语法,所以如果我想写 for AND NOT- 使用上面的词,我想匹配 'small and 'goods' 但不匹配 'leather. a 和 b 但不是 c
猜你喜欢
  • 1970-01-01
  • 2011-09-29
  • 2023-01-02
  • 2021-06-04
  • 1970-01-01
  • 1970-01-01
  • 2012-08-21
  • 1970-01-01
  • 2014-03-22
相关资源
最近更新 更多