0

I have the following data stored in the database column xyz:

a=222223333;b=433333657675457;c=77777;
a=52424252424242;d=5353535353;b=7373737373;

There is no requirement that b value should always be there but if b value is present I have to retrieve the value following b=.

I want to retrieve the value of b using regular expressions in Oracle and I am unable to do it.

Can anyone please help me find a solution for this?

1
  • 2
    Is there anything that you have tried /(re)searched? Commented Jan 27, 2018 at 5:58

4 Answers 4

1

I suggest using Oracle built-in function REGEXP_SUBSTR which returns a substring using regular expressions. According to the example you posted, the following should work.

SELECT REGEXP_SUBSTR(xyz, 'b=\d+;') FROM your_table
Sign up to request clarification or add additional context in comments.

1 Comment

It doesnt work if the key is, say, "ab" when you are looking for just "b"
1

You can use regexp_substr:

select substr(regexp_substr(';' || xyz, ';b=\d+'), 4) from your_table;

Concatenation with ; is to distinguish between key-value pair with key say 'ab' and 'b'.

Comments

1

Use a Subexpression to Select a Substring of Your REGEXP_SUBSTR Matching Pattern

My match pattern, 'b=(\d+);', includes the parenthesis which mark this subexpression which is the last parameter of REGEXP_SUBSTR.

If you look at the 12c documentation, you will see that the third example uses a subexpression.

The escaped d just is a regular expression shorthand to indicate that we are looking for digits and the plus symbol is a quantifier indicating 1 or more digits.

SCOTT@db>WITH smple AS (
  2      SELECT
  3          'a=52424252424242;d=5353535353;b=7373737373;' dta
  4      FROM
  5          dual
  6  ) SELECT
  7      dta,
  8      regexp_substr(a.dta,'b=(\d+);',1,1,NULL,1) subexp
  9    FROM
 10      smple a;
DTA                                           subexp
---------------------------------------------------------
a=52424252424242;d=5353535353;b=7373737373;   7373737373

1 Comment

Yes,this is failing when there is no b.Please also provide me solution when there is no b.Thank you.
-2

enter image description hereabove solution is working in all the cases even if b contains alphanumeric

1 Comment

This will fail if there is no b value. Also please post actual code, not screenshots.

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.