【问题标题】:Limit regexp_substr responses when unwanted characters appear in string (return null responses)当字符串中出现不需要的字符时限制 regexp_substr 响应(返回空响应)
【发布时间】: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 是我第一次尝试限制响应,但没有成功。

【问题讨论】:

    标签: sql regex oracle case regexp-substr


    【解决方案1】:

    这是一个解决方案,它使用两次对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
    
    科尔 | 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.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        • 2018-08-16
        • 2022-01-10
        • 1970-01-01
        • 2012-08-03
        • 1970-01-01
        • 2022-01-25
        相关资源
        最近更新 更多