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

Posted

技术标签:

【中文标题】当字符串中出现不需要的字符时限制 regexp_substr 响应(返回空响应)【英文标题】:Limit regexp_substr responses when unwanted characters appear in string (return null responses) 【发布时间】:2019-12-04 00:11:48 【问题描述】:

当某些字符出现在字符串中时尝试限制响应- 即我想要“牛去散步” 不过

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 是我第一次尝试限制响应,但没有成功。

【问题讨论】:

【参考方案1】:

这是一个解决方案,它使用两次对regexp_like() 的调用来确保值与条件匹配:

select
    col,
    case 
        when 
            regexp_like(col, 'cow[^.]1,40walk', 'i')
            and not regexp_like(col, '^.1,20bad', '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,40walk', 'i')
            and not regexp_like(col, '^.1,20bad', 'i')
        then col
    end new_col
from test
科尔 | NEW_COL :------------------------------------------------------------ | :------------------------ 牛去散步了|牛去散步了 BAD 卑鄙的牛去散步 | 牛回来了。但是狗去散步了|

【讨论】:

【参考方案2】:

选择 col 在整行中不包含标点符号并且在前 20 个字符中不包含单词 BAD 的行。单词 BAD 前面是行首或空格,后跟空格或行尾以处理这些情况。确保您的测试数据处理所有情况,尤其是那些您不期望的情况,例如字符串 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.

【讨论】:

以上是关于当字符串中出现不需要的字符时限制 regexp_substr 响应(返回空响应)的主要内容,如果未能解决你的问题,请参考以下文章

为啥需要urlEncode

1.9.11

Excel VBA:将变体数组返回到选定范围时需要 255 转置字符限制的解决方法

vue组件

CCF 字符串匹配

ccf--20140903--字符串匹配