简体   繁体   中英

Oracle Regexp_replace multiple occurrence

Hi I want to append a letter C to a string if it starts with a number . Also if it has any punctuation then replace with underscore _ Eg : 5-2-2-1 ==> C5_2_2_1

I tried ,but I am not able to replace the multiple occurrence of the punctuation. I am missing some simple thing, I cant get it.

SELECT  REGEXP_REPLACE('9-1-1','^(\d)(-),'C\1_' ) FROM DUAL;
SELECT case when REGEXP_LIKE('9-1-1','^[[:digit:]]') then 'C' END 
       || REGEXP_REPLACE('9-1-1', '[[:punct:]]', '_')
FROM DUAL;

[:digit:] any digit
[:punct:] punctuation symbol

if you have a lot of rows with different values then try to avoid regex:

SELECT case when substr('9-1-1',1,1) between '0' and '9' then 'C' end
       || translate('9-1-1', ',.!-', '_')
FROM DUAL;

Check here for example: Performance of regexp_replace vs translate in Oracle?

Try this:

select (case when substr(val, 1, 1) between '0' and '9' then 'C' else '' end) ||
       regexp_replace(val, '([-+.,;:'"!])', '_')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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