1

I am trying to parse a where clause in a string to return the name of the lookup_type. The where clause could be anything.

This is what I currently have:

select regexp_substr('where lookup_type = ''THE MILK'' ', 'lookup_type(\s*)=(\s*)''(^''*)''')
from dual

For some reason this does not work. Also how can I only return THE MILK from the regex?

2 Answers 2

2

You may use

select regexp_substr('where lookup_type = ''THE MILK'' ', 'lookup_type\s*=\s*''([^'']+)''', 1, 1, NULL, 1) as result from dual

See the online demo

Details

  • lookup_type - matches a literal substring
  • \s*=\s* - matches = enclosed with 0+ whitespaces
  • '' - matches a '
  • ([^'']+) - matches and captures 1 or more chars other than '
  • '' - matches a '
Sign up to request clarification or add additional context in comments.

1 Comment

Correct, ^ means "start of string"; [^ means "not one of the characters in brackets". As we don't need to capture anything, we can even replace ([^'']+) by [^'']+.
0

You could escape some quotes with q notation( alternative quoting mechanism)

SELECT REGEXP_SUBSTR(q'#where lookup_type = 'THE MILK'#',
              q'#lookup_type\s*=\s*'([^']+)#', 1, 1, 'i', 1) name
FROM   dual  ;

DEMO

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.