3

I am using the following query in Oracle 11.2.0.3.0 / Toad for Oracle 11.6.1.6:

select  regexp_replace('000010PARA197427'
                      ,'([0-9]*)([A-Z]*)([0-9]*)'
                      ,'\3-\2-\1') from dual

Rather than getting what I expected, 197427-PARA-000010. I get 197427-PARA-000010-- as a result.

If I change the query to:

select  regexp_replace('000010PARA197427'
                      ,'([0-9]*)([A-Z]*)([0-9]*)'
                      ,'\3-c\2-c\1') from dual

I then get 197427-cPARA-c000010-c-c for the result.

it's like all the literals are getting appended to the end of the result.

Any help would be much appreciated.

1 Answer 1

4

Not exactly sure why this is happening, but since you only have * quantifiers and no anchoring, maybe you're getting an empty match (or something like that).

Anchoring the pattern (/^...$/) seems to work. Using + rather than * for any of the quantifier also works for this sample.

SQL> select regexp_replace('000010PARA197427'
                          ,'([0-9]+)([A-Z]*)([0-9]*)'
                          ,'\3-\2-\1') foo from dual ;

FOO
------------------
197427-PARA-000010

SQL> select regexp_replace('000010PARA197427'
                          ,'^([0-9]*)([A-Z]*)([0-9]*)$'
                          ,'\3-\2-\1') foo from dual ;

FOO
------------------
197427-PARA-000010
Sign up to request clarification or add additional context in comments.

Comments

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.