使用 regexp_substr 捕获两个单词之间的所有字符(包括字符串的前端和结尾

using regexp_substr to capture all characters between two words (including the front and end of the string

所以我主要使用 regexp_like 而不是 regexp_substr- 但在下面的字符串中

'abc 3/4/16 blah blah 3/7/16 imp 2/8/15 xxx cc2' 
'3/10/18 bla bla imp-3/9/17 xfe 334 3/4/13'

I want to capture 
imp 2/8/15 xxx cc2
imp-3/9/17 xfe 

在 regexp_like 中,它只是 regexp_like('abc 3/4/16 blah blah 3/7/16 imp 2/8/15 xxx cc2','(imp).{0,20}(cc2|xfe)') 但它不是与 regexp_substr

的工作方式相同

我尝试使用下面的代码 w/o 成功。

with test (id, col) as
      (select 1, 'abc 3/4/16 blah blah IMP 3/7/16'                     from dual union all
      select 2, 'abc 3/4/16 blah blah 3/7/16 imp 2/8/15 xxx cc2'      from dual union all
      select 3, 'xxx 3/5/18 ccdd 234 imp happened on 5/8/19 some 23f' from dual union all
      select 4, '3/10/18 bla bla imp-3/9/17 xfe 334 3/4/13 x'         from dual
      )
   select id,
      regexp_substr('(imp).*(xxx|xfe)',1) result
  from test;

需要注意大小写!

感谢建议。 谢谢

您根本没有在查询中使用 col

试试这个:

with test (id, col) as
      (select 1, 'abc 3/4/16 blah blah IMP 3/7/16'                     from dual union all
      select 2, 'abc 3/4/16 blah blah 3/7/16 imp 2/8/15 xxx cc2'      from dual union all
      select 3, 'xxx 3/5/18 ccdd 234 imp happened on 5/8/19 some 23f' from dual union all
      select 4, '3/10/18 bla bla imp-3/9/17 xfe 334 3/4/13 x'         from dual
      )
   select id,
      regexp_substr(col, '(imp).*(xxx|xfe)',1, 1) result -- use occurance as 1, last parameter
  from test;

db<>fiddle demo

干杯!!