I have a scenario where I have to replace the value in comma separated string based on the position. Below are the cases:
Case 1:
select regexp_replace('1,2,3','[^,]+',5,1,3)
from dual;
Returns correct result: 1,2,5
Case 2:
select regexp_replace('1,,3','[^,]+',5,1,3)
from dual;
Returns result: 1,,3
Expected Result: 1,,5
@Gary_W has written about the problem with using that regex pattern to split strings, precisely because of how it treats empty tags
So I tried Case 3:
select regexp_replace('1,,3','(.*?)(,|$)',5,1,3)
from dual;
Returns Expected Result: 1,,5
But if I try this Case 4:
select regexp_replace('1,2,3','(.*?)(,|$)',5,1,2)
from dual;
Returns result: 1,53
Expected Result: 1,5,3
I understand that I am doing something wrong with regular expression.Is there a way to make regexp_replace work in all above scenarios?