0

so we have a string of data which may contain something like this:

(<acronym class=\"cticker\">UST</acronym>)

We want to modify it slightly so it is this:

(<acronym class=\"cticker\">UST-USD</acronym>)

I started to play with some REGEXP_REPLACE like this:

SELECT REGEXP_REPLACE(json_content, '\(<acronym class=[\\]+\"cticker[\\]+\">([a-zA-Z0-9]{1,5})</acronym>\)',

But now I got stuck, not sure how to make the right side of this expression. Here, the ticker value, UST can be anything. So, just need to append "- USD" to it, like I'm trying to do above.

Was hoping someone is better at regular expressions that me.

3
  • Use \1 in the replacement string to copy the capture group. So it becomes \1-USD to add -USD to it. Commented May 20, 2022 at 17:19
  • 1
    See the example in the documentation Commented May 20, 2022 at 17:19
  • Your input string looks almost like an XML document. So, first, why is it not an XML document (or XML content, anyway) - for example, why is it enclosed in parentheses? And second, why regexp functions, and not proper XML functions? Why even make the effort to have the data as almost-XML if you aren't going to use XML tools anyway? Commented May 20, 2022 at 17:59

1 Answer 1

0

Here is the code. You can use backreferences to answer the question.

SQL>
SELECT COL,REGEXP_REPLACE(col,'(<acronym class=[\\]"cticker[\\]">UST)([<]) 
(\/acronym>)','\1-USD\2\3') AS RESULT
FROM 
(
SELECT '<acronym class=\"cticker\">UST</acronym>' AS COL FROM DUAL
)
RESULT
------------------------------------------------------------------------
            COL                                         RESULT
<acronym class=\"cticker\">                  <acronym class=\"cticker\">
       UST</acronym>                               UST-USD</acronym>
                                              
SQL>
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.