zundarz
zundarz

Reputation: 1594

How can I change this regex to get desired output?

Here is Fiddle.

Desired Output

Buy flood insurance. Act now.
Buy flood insurance. Call now.
Buy flood insurance. No change.

Here's my attempt:

    with data_row as (
    select 'or buy flood insurance. Act now.' as disclaimer from dual union  all
    select 'or Buy flood insurance. Call now.' as disclaimer from dual union  all
    select  'Buy flood insurance. No change.' as disclaimer from dual )
    select case        
        when  regexp_like(disclaimer,'^or (B|b)uy flood insurance.*') then 
              regexp_replace(disclaimer,'^or (B|b)uy flood insurance.*','Buy flood insurance.')      
       else disclaimer
       end as revised
 from data_row

Upvotes: 2

Views: 46

Answers (1)

Bohemian
Bohemian

Reputation: 425208

Just use like with lower() to find what needs changing and overwrite the B with a hard-coded upper case if needed:

select 
   case 
     when lower(message) not like 'or b%' then message
     else 'B' || substr(message, 5)
   end message
from disclaimer

See SQLFiddle

Upvotes: 1

Related Questions