0

I have following SQL query (Oracle 18c):

SELECT 
    --FIRST
    translate(
        ' sOmE tEsT


                   eNdOfLiNe', 
        chr(10)||chr(11)||chr(13), 'replText'
    ) "Result1",

    --SECOND
    regexp_replace(
        ' sOmE tEsT


                   eNdOfLiNe',
        '[\x0A|\x0B|`\x0D]', 'replText'
    ) "Result2",

    --THIRD
    regexp_replace(
        ' sOmE tEsT


                   eNdOfLiNe',
        '[\r\n\t]', 'replText', 1, 0
    ) "Result3"
FROM dual

What I would like to do is replace all tabs, return carriages and new line indicators with new string but it seems like regexp replace is not working (returns initial text). I am really sorry about formatting but I need to handle text in exact format as above with \r \n \t mixed chars.

Here is fiddle: https://dbfiddle.uk/?rdbms=oracle_18&fiddle=63834f9bcab93136635366f18c375b13

I am learning Oracle right now and don't understand why second and third solution returns initial text. The first solution seems to work but I would like to achieve the same effect in SECOND and THIRD solution. What I missed?

1
  • 1
    If REGEXP_REPLACE() fails to match the regex to the input string, the input string is returned. Note this is in contrast to REGEXP_SUBSTR(), where if the match is not found NULL is returned. Commented Apr 23, 2020 at 13:08

2 Answers 2

1

I'm pretty sure Oracle does not allow escape sequences in a character class. I believe this is what you have to do. In response to your comment on another answer here and as you are learning, regex is most definitely not regex. Especially Oracle's implementation.

EDIT to explain the regex: The regex pattern is building a string of a regex character class containing 3 characters, hence the concatenation. You can't just have escape characters in the regex as then regex would take those characters as part of the character class pattern itself.

SELECT REGEXP_REPLACE(
' sOmE tEsT


                       eNdOfLiNe', '['||CHR(9)||CHR(10)||CHR(13)||']', 'X') Result3
FROM dual;

RESULT3                       
------------------------------
 sOmE tEsTXXXXXXXX   eNdOfLiNe
1 row selected.
Sign up to request clarification or add additional context in comments.

Comments

0

You can try the below using similar format as translate

  select  regexp_replace(
   ' sOmE tEsT


               eNdOfLiNe',
    chr(10)||'|'||chr(11)||'|'||chr(13), 'replText') "Result3"
    FROM dual

4 Comments

Sure, but why regex doesn't work if some examples of regex are published on Oracle doc page?
Can you provide the link to oracle docs where the newline regexp expression is mentioned
There are not explicit examples of \r \n \t but regex is regex right? There are some examples in web f.e. chrismemo.wordpress.com/2018/05/18/…
@AdamMrozek That example mentioned in the link above doesn't work (at least on 11g), even after one fixes the typo (remove the backquote).

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.