2

I'm new using REGEXP_REPLACE(). I want to replace 0 to '-' digit, for example : 30000000176215001500 we get 3-176215001500

I tried using regexp_replace('30000000176215001500','([0])','-'), but it changes all 0s to -.

This is what I expect:

  1. 30000001174934177910 : 3-1174934177910
  2. 30000000174934177910 : 3-174934177910
  3. 301873130520 : 3-1873130520
  4. 300173130520 : 3-173130520

1 Answer 1

5

Consider:

regexp_replace(mycol,'0+','-', 1, 1)

Rationale: the fifth parameter, when greater than 0, specifies wich occurence should be replaced; when it is set to 0, all occurences are replaced.

Other notable changes to your original regex:

  • parentheses define capturing group; since you don't need to capture, there are superfluous
  • brackets define a character class; not needed since you are matching on a single character

Demo on DB Fiddle:

with a as (
    select '30000001174934177910' mycol from dual
    union all select '30000000174934177910' from dual
    union all select '301873130520' from dual
    union all select '300173130520' from dual
)
select mycol input, regexp_replace(mycol,'0+','-', 1, 1) output from a
INPUT                | OUTPUT         
:------------------- | :--------------
30000001174934177910 | 3-1174934177910
30000000174934177910 | 3-174934177910 
301873130520         | 3-1873130520   
300173130520         | 3-173130520    
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, sorry, you're right - I completely missed that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.