3

I am trying to extract a value between the brackets from a string.

Here(How to extract a string between brackets in oracle sql query), it is explains how to do.

But in my situation, the string has 2 lines. With this way, I get only NULL.

SELECT REGEXP_SUBSTR('Gupta, Abha (01792)', '\((.+)\)', 1, 1, NULL, 1) FROM dual --01792

SELECT REGEXP_SUBSTR('Gupta, Abha (01
                 792)', '\((.+)\)', 1, 1, NULL, 1) FROM dual -- NULL

I known that i can remove the break line symbol and then use regex_substr but i need to keep the break line symbol

3
  • 3
    What you need is another option, 'n' or 'm', it depends on your data which one to use. Check Table 3-2 on docs.oracle.com/cd/E11882_01/appdev.112/e41502/adfns_regexp.htm Commented Dec 14, 2019 at 8:54
  • Sample data and expected results would help a lot. Commented Dec 14, 2019 at 9:13
  • @Daeron you should convert your comment into an answer. Working fiddle Commented Dec 14, 2019 at 9:17

1 Answer 1

2

I would adress this with the following regex:

\(([^)]*)\

This makes use of a custom character class, [^)], which means: everything but a closing parenthese. This way, you do not have to worry about line breaks (since, obviously, a line break is not a closing parenthese), or any other special character:

Demo on DB Fiddle:

SELECT REGEXP_SUBSTR('Gupta, Abha (01
                 792)', '\(([^)]*)\)') res FROM dual
| RES                  |
| :------------------- |
| (01                  |
|                 792) |
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.