0

Is it possible to replace multiple occurrences of the pattern with a substring using REGEXP_REPLACE function in Oracle. I tried multiple permutation & combination of the regex expression, but somehow it didn’t work out correctly. Superficially it looks doable but not very sure.. any ideas or pointer to try out. Sample input/output below

INPUT_STRING =  SELECT @DATA:T.ID:ID@, @DATA:T.NAME:NAME@, @DATA:T.ADDRESS:ADDRESS@, @DATA:T.CREATED_DATE:CREATED_DATE@ FROM TABLE_NAME T
OUTPUT_STRING = SELECT T.ID ID, T.NAME NAME, T.ADDRESS ADDRESS FROM TABLE_NAME T

In essence, we need to achieve below in the fixed pattern

@DATA:xx.yy:zz@, where xx is table, yy is column name, zz column alias   
  1. Remove @DATA: from the beginning of string
  2. Remove @ at the end of string
  3. Replace : with space
1

2 Answers 2

1

I think you want something like

select regexp_replace(txt,'@DATA:(\w+\.(\w+)):(\2)@', '\1 \2' ) from ( 
   select 'SELECT @DATA:T.ID:ID@, @DATA:T.NAME:NAME@, @DATA:T.ADDRESS:ADDRESS@, @DATA:T.CREATED_DATE:CREATED_DATE@ FROM TABLE_NAME T' as txt 
   from dual);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, it worked. I was struggling with the sub expression. I tried lot of combos.. but this was it, which I missed :)
Also, I tried another pattern - @DATA:(.+?):(.+?)@. Are there any repercussions with this?
Well, It depends of what you mean as repercussions. you specify good pattern ".+?" - will try to find "string" with least length and you may use it. with the same query regexp_replace(txt,'@DATA:(.+?):(.+?)@', '\1 \2' )
0

somthing like this:

 replace(regexp_replace(str, '^@DATA:|@$'), ':', ' ') 

Test:

select str, replace(regexp_replace(str, '^@DATA:|@$'), ':', ' ') 
from ( select '@DATA:xx.yy:zz@' as str from dual);

3 Comments

^ - is begin of the string $ - is the end. If it in middle of line it will not work
correct, but OP wanted to remove only from the begin and the end
Thanks for your suggestion, it worked for the given pattern, however, when I am passing the entire string, it's not giving expected output.

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.