当不需要的字符出现在字符串中时限制 regexp_substr 响应(return 空响应)

Limit regexp_substr responses when unwanted characters appear in string (return null responses)

尝试在字符串中出现某些字符时限制响应- 即我想要 'The cow went for a walk' 然而

1 if within 20 characters the word 'BAD' appears such as the "The BAD mean cow went for a walk" it would return a null value
2. The same if there was punctuation in the middle of the sentence, so that
     "The cow came back. But the dog went for a walk" would not return a value- 

代码如下。

with test (id, col) as (
  select 1, 'The cow went for a walk'                         from dual union all --want this
  select 2, 'The BAD  mean cow went for a walk'                 from dual union all   --do not want this
  select 3, 'The cow came back. But the dog went for a walk'    from dual) --do not want this 
  select id, col, 
       regexp_substr(col,'(cow).{1,40}(walk)',1,1,'i') rs
regexp_substr(col,'(cow).{1,40}(([^.])(walk))',1,1,'i') rs2
      from test

RS2是我第一次尝试w/o限制响应成功

这是一个解决方案,它使用两次调用 regexp_like() 来确保值符合条件:

select
    col,
    case 
        when 
            regexp_like(col, 'cow[^.]{1,40}walk', 'i')
            and not regexp_like(col, '^.{1,20}bad', 'i')
        then col
    end new_col
from test

第一个函数调用确保字符串包含作品 'cow',后跟 'walk',最多 40 个字符,中间没有点。第二次调用消除了前 20 个字符中包含单词 'bad' 的字符串。

Demo on DB Fiddle:

with test (id, col) as (
    select 1, 'The cow went for a walk' from dual 
    union all 
    select 2, 'The BAD  mean cow went for a walk' from dual
    union all
    select 3, 'The cow came back. But the dog went for a walk' from dual
)
select
    col,
    case 
        when 
            regexp_like(col, 'cow[^.]{1,40}walk', 'i')
            and not regexp_like(col, '^.{1,20}bad', 'i')
        then col
    end new_col
from test
COL                                            | NEW_COL                
:--------------------------------------------- | :----------------------
The cow went for a walk                        | The cow went for a walk
The BAD  mean cow went for a walk              | null                   
The cow came back. But the dog went for a walk | null                   

Select col 在整行中不包含标点符号并且在前 20 个字符中不包含单词 BAD 的行。单词 BAD 位于行首或 space 之前,后跟 space 或行尾以处理这些情况。确保您的测试数据处理所有情况,尤其是您不希望出现的情况,例如字符串 BAD 是单词的一部分!

with test (id, col) as (
  select 1, 'The cow went for a walk'                         from dual union all -- want this
  select 2, 'The BAD mean cow weBADnt for a walk'             from dual union all -- do not want this
  select 3, 'BAD The cow went for a walk'                     from dual union all
  select 4, 'The cow went forBAD a walk'                     from dual union all
  select 5, 'The cow went fo BAD a walk'                     from dual union all
  select 6, 'The cow went for a walk BAD'                    from dual union all  
  select 7, 'The cow came back. But the dog went for a walk'  from dual           -- do not want this
)  
select id, col
from test
where NOT regexp_like(substr(col,1,20), '((^| )BAD( |$))')
and   NOT regexp_like(col, '[[:punct:]]');


    ID COL                                           
---------- ----------------------------------------------
     1 The cow went for a walk                       
     4 The cow went forBAD a walk                    
     6 The cow went for a walk BAD                   

3 rows selected.