0

I have a query:

select ITEM_ID from system_items where id=4020;

I want a regular expression that takes the above query as input and matches for pattern "id=" and returns 4020.

Please let me know if you have any suggestions, as I have been trying with REGEXP_SUBSTR in Oracle and couldn't get it.

2 Answers 2

1

REGEX_SUBSTR won't allow a look-behind like (?<=id=\s*)\d+ so I suspect you need to do this in two operations. First get id=4020, then strip the id=.

One possible way of doing that would be:

REGEXP_SUBSTR(REGEXP_SUBSTR(a, 'id=\s*\d+'), '\d+')

SQLFiddle

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

Comments

-1

This should do it

 /id=(\d+)/

id is literal match
() are used for making the capture groups
\d is more numbers 
+ ensures 1 or more

demo here http://rubular.com/r/GBxfhID5hS

4 Comments

Hi aelor, I have tried it in this way DBMS_OUTPUT.PUT_LINE(REGEXP_SUBSTR('select ITEM_ID from system_items where id=4020;','/id=(\d+)/')); It doesn't give any output :(
Can you try this DBMS_OUTPUT.PUT_LINE(REGEXP_SUBSTR('select ITEM_ID from system_items where id=4020;','\d+$'));
Aelor, say if the query is "select ITEM_ID from system_items where id=4020 order by field1;" still this could work ?
i think you need to remove the start and end '/' in my regex

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.