1

I have an regex like

select regexp_substr('some stuff TOTAL_SCORE<518>some stuff OTHER_VALUE<456> foo <after>', 'TOTAL_SCORE<(\d{3})>', 1, 1, NULL, 1) from dual which can return a value for a single capturing group. How can I instead return all the capturing groups as an additional column? (string concat of results is fine)

select regexp_substr('some stuff TOTAL_SCORE<518> TOTAL_SCORE<123>some stuff OTHER_VALUE<456> foo <after>', 'TOTAL_SCORE<(\d{3})>') from dual
2
  • There is only one capturing group in your regular expression and it will only make a single match. Can you give an example where there are either multiple capturing groups or multiple matches and your expected output. Commented Aug 7, 2017 at 9:01
  • Sorry now with TOTAL_SCORE<518> TOTAL_SCORE<123> there should be 2 matches Commented Aug 7, 2017 at 9:02

1 Answer 1

3

Query 1:

-- Sample data
WITH your_table ( value ) AS (
  SELECT 'some stuff TOTAL_SCORE<518>some stuff OTHER_VALUE<456> foo <after>' FROM DUAL
)
-- Query
SELECT REGEXP_REPLACE(
         value,
         '.*TOTAL_SCORE<(\d{3})>.*OTHER_VALUE<(\d{3})>.*',
         '\1,\2'
       ) As scores
FROM   your_table

Output:

SCORES
-------
518,456

Query 2:

-- Sample data
WITH your_table ( value ) AS (
  SELECT 'some stuff TOTAL_SCORE<518> TOTAL_SCORE<123> some stuff OTHER_VALUE<456> foo <after>' FROM DUAL
)
-- Query
SELECT l.column_value As scores
FROM   your_table t,
       TABLE(
         CAST(
           MULTISET(
             SELECT TO_NUMBER(
                      REGEXP_SUBSTR(
                        t.value,
                        'TOTAL_SCORE<(\d{3})>',
                        1,
                        LEVEL,
                        NULL,
                        1
                      )
                    )
             FROM   DUAL
             CONNECT BY LEVEL <= REGEXP_COUNT( t.value, 'TOTAL_SCORE<(\d{3})>' ) 
           ) AS SYS.ODCINUMBERLIST
         )
       ) l;

Output:

SCORES
-------
    518
    123
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.