简体   繁体   English

Oracle中的REGEXP_REPLACE

[英]REGEXP_REPLACE in Oracle

I need to use REGEXP_REPLACE to do the following : 我需要使用REGEXP_REPLACE来执行以下操作:

 If word starts with 'ABCD' then replace first four(4) chars with 'FFFF'
    else
 If word starts with 'XYZ' then replace first three(3) chars with 'GGG'

How do I use REGEXP_REPLACE to do conditional replace ? 如何使用REGEXP_REPLACE进行条件替换?

You can use case and string operations: 您可以使用case和字符串操作:

select (case when word like 'ABCD%'
             then 'FFFF' || substr(word, 5)
             when word like 'XYZ%'
             then 'GGG' || substr(word, 4)
             else word
        end) as new_word

If it has to be REGEXP_REPLACE you'll have to combine two function calls: 如果必须是REGEXP_REPLACE ,则必须组合两个函数调用:

REGEXP_REPLACE( 
  REGEXP_REPLACE(word,'^ABCD','FFFF')
  ,'^XYZ', 'GGG')

But I would prevere Gordon's case approach... 但我会优先考虑戈登的case方法......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM