Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REGEXP_REPLACE back-reference

I have went through the documentation of Oracle regarding REGEXP_REPLACE function, but it didn't work as expected

Here is what I got:


My target: concatenate a character for each set of continuous numbers

My input:

(1101 + 1102) * 1103 + 1104 + 1105

Expected output:

(T1101 + T1102) * T1103 + T1104 + T1105

Code:

SELECT
  REGEXP_REPLACE('(1101 + 1102) * 1103 + 1104 + 1105',
                 '[0-9]+',
                 'T\1') "REZ"
  FROM DUAL;

Result I've got:

(T\1 + T\1) * T\1 + T\1 + T\1

I've searched many Stack-overflow posts, and they all seems to have this scenario working without any problems regarding the \1

Also tried different places to test the code: Tod for oracle, SqlPlus and another application, they all gave me the same results.

So, would anyone please tell me whats wrong with my code ?

Note:

I don't suspect that my regular expression has any problems, tested before in a different scenario in oracle, and its working. Plus, matches are found, but the replacement part is not working.

References of some Stack-overflow posts:

Ref1, Ref2, Ref3

Reference to Oracle Documentation:

REGEXP_REPLACE

like image 274
sameh.q Avatar asked May 22 '26 22:05

sameh.q


1 Answers

\1 is back reference that looks for previous matched groups and groups can be captured by enclosing it inside the parenthesis (...)

It should be

([0-9]+)
like image 170
Braj Avatar answered May 24 '26 13:05

Braj