Oracle regexp模式匹配但不是ab

时间:2017-11-15 18:46:15

标签: regex oracle regex-negation

我从oracle文档中看到,我可以做一个| b,它将匹配字符'a'或字符'b'的所有实例。我想知道regexp中是否有可能有'a'而不是'b',例如匹配'a'的所有实例,除非a后跟'b'。

所以这3个字符串

  1. AAAA
  2. ACAD
  3. AAAB
  4. 我希望匹配1和2而不是3。

1 个答案:

答案 0 :(得分:1)

你可以尝试:

with x as (
  select 'AAAA' as str from dual
  union all
  select 'ACAD' as str from dual
  union all
  select 'AAAB' as str from dual
)
select * from x
where regexp_like(str, '[aA][^bB]')
and NOT regexp_like(str, '[aA][bB]')

输出:

AAAA
ACAD

注意:如果你不在乎A是否跟着其他一些非B字符,你可以这样做:

with x as (
  select 'AAAA' as str from dual
  union all
  select 'ACAD' as str from dual
  union all
  select 'AAAB' as str from dual
)
select * from x
where regexp_like(str, '[aA]')
and NOT regexp_like(str, '[aA][bB]')

匹配“AAAA”或“XYZA”