2

I'm trying to extract 456 from the string :123:456: as follows:

select regexp_substr(':123:456:', ':(\d+):', 1, 2, 'i', 1) from dual

However, this query returns null. What am I doing wrong?

András Váczi
31.8k13 gold badges103 silver badges152 bronze badges
asked Sep 5, 2012 at 16:14
0

2 Answers 2

6

This works fine:

select regexp_substr(':123:456:', '(\d+):', 1, 2, 'i', 1) from dual;

I think yours fails because the opening and closing colons won't get matched by both occurrences (because the first match is greedy).

answered Sep 5, 2012 at 16:43
-2
select regexp_substr(':123:456:','[^:]+',2,2) from dual
Aaron Bertrand
182k28 gold badges407 silver badges626 bronze badges
answered Jul 9, 2013 at 13:52
2
  • 1
    If this is substantially different from the accepted answer posted by Phil 10 months ago, please add some context explaining why. Commented Jul 9, 2013 at 13:58
  • This will match on :123:abc: which doesn't sound desirable, but fail to match on ::456: which might be. Commented Jul 9, 2013 at 17:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.