0

I'm trying to do a regex match to return a substring between a start and end point.

Given the following table:

WITH test AS (SELECT 'ABCD_EFGH_THIS_IJKL' AS thetext FROM DUAL
UNION SELECT 'ABAB CDCD EG BCD' FROM DUAL)
SELECT *
FROM test

I would want to return the results:

'THIS'
NULL

So it would match THIS in the first string, and nothing in the second string.

For this is safe to assume that ABCD_EFGH preceeds the text i want to match, and _ follows the text I want to match.

Thanks for any help!

EDIT: This needs to work on 10g. Sorry for not making that clear turbanoff.

1 Answer 1

1

use REGEXP_SUBSTR with 11g

WITH test AS (SELECT 'ABCD_EFGH_THIS_IJKL' AS thetext FROM DUAL
UNION SELECT 'ABAB CDCD EG BCD' FROM DUAL)
SELECT REGEXP_SUBSTR( TEST.THETEXT, 'ABCD_EFGH_([^_]*).*', 1, 1, 'i', 1)
FROM test

Edit

This can be done without using regular expressions.

WITH test AS (SELECT 'ABCD_EFGH_THIS_IJKL' AS thetext FROM DUAL
UNION SELECT 'ABAB CDCD EG BCD' FROM DUAL)
select TEST.thetext
     , instr(TEST.thetext, 'ABCD_EFGH_') + length('ABCD_EFGH_')  START_POS
     , instr(TEST.thetext, '_', length('ABCD_EFGH_') + 1) END_POS
     , substr
            (TEST.thetext
            ,instr(TEST.thetext, 'ABCD_EFGH_') + length('ABCD_EFGH_') --START_POS
            ,instr(TEST.thetext, '_', length('ABCD_EFGH_') + 1) - (instr(TEST.thetext, 'ABCD_EFGH_') + length('ABCD_EFGH_')) --END_POS - START_POS
            ) RESULT
FROM test
Sign up to request clarification or add additional context in comments.

2 Comments

I'm only on 10g unfortunately and I get an error saying too many arguments.
I knew I should have dropped regular expressions and just done it the old fashioned way. Thanks.

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.