1

Given a string of key-value pairs: /* USER='Administrator'; UNV='Universe'; DOC='WebIntellignceReport'; */

My goal is to extract values associated with the USER, UNV, and DOC keys.

Using a pattern of (?<=UNV=')(.*?)(?='), I get the expected value of Universe associated the UNV key (Fiddle).

However, when I use the pattern with REGEXP_SUBSTR, I get a NULL:

SELECT  text
        ,REGEXP_SUBSTR(text,'(?<=UNV='')(.*?)(?='')') UNV
FROM (
  SELECT  '/* USER=''Administrator''; UNV=''Universe''; DOC=''WebIntellignceReport''; */' as text 
  FROM    dual
) v  

What am I missing?

4
  • 1
    Oracle regex does not support lookarounds. Try REGEXP_SUBSTR(text,'UNV=''(.*?)''', 1, 1, NULL, 1) UNV Commented Feb 7, 2018 at 14:32
  • 2
    You are testing these expressions using pcrp(php) regexp engine. This won't work on Oracle because it's regexp implementation is limited and doesnt support many features, see this link for details: regular-expressions.info/oracle.html Commented Feb 7, 2018 at 14:49
  • 1
    Moreover, the query you posted (EXACTLY as posted) will not even pass compilation, since you are not handling single-quotes properly in the input string. If you post something for us to test, please test it yourself first, and fix it if it needs fixing. Commented Feb 7, 2018 at 14:51
  • @mathguy, upon closer inspection, my IDE (Datagrip) did correct the /* ... */syntax, so it worked. Must have copied different source. Commented Feb 7, 2018 at 15:15

2 Answers 2

1

You may extract the contents of group 1:

SELECT  text, REGEXP_SUBSTR(text,'UNV=''(.*?)''', 1, 1 ,NULL, 1) UNV
FROM (
  SELECT  '/* USER=''Administrator''; UNV=''Universe''; DOC=''WebIntellignceReport''; */' as text 
  FROM    dual
) v

See the online demo.

enter image description here

With UNV='(.*?)' , you may extract just what is between the closest single quuotes afterUNV=.

Sign up to request clarification or add additional context in comments.

Comments

0

I think the easiest thing to do is just grab the whole key-value pair using REGEXP_SUBSTR, and then do another substr to pull out the value you want.

with v as (select '/* USER=''Administrator''; UNV=''Universe''; DOC=''WebIntellignceReport''; */' as text from dual)
select text, key_val, substr(key_val, instr(key_val, '''')+1, length(key_val)-instr(key_val, '''')-2)
from (
    select text, 
    regexp_substr(text, ' UNV=''[^'']*'';') key_val 
    from v);

Output:

TEXT                                                                    KEY_VAL                                                                 VAL                                                                    
----------------------------------------------------------------------- ----------------------------------------------------------------------- -----------------------------------------------------------------------
/* USER='Administrator'; UNV='Universe'; DOC='WebIntellignceReport'; */  UNV='Universe';                                                        Universe                                                               

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.