正则表达式oracle sql返回所有捕获组

时间:2017-08-07 08:55:57

标签: sql regex oracle concatenation capturing-group

我有像

这样的正则表达式

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,可以返回单个捕获组的值。 我怎样才能将所有捕获组作为附加列返回? (字符串结果很好)

select regexp_substr('some stuff TOTAL_SCORE<518> TOTAL_SCORE<123>some stuff OTHER_VALUE<456> foo <after>', 'TOTAL_SCORE<(\d{3})>') from dual

1 个答案:

答案 0 :(得分:2)

查询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

<强>输出

SCORES
-------
518,456

查询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;

<强>输出

SCORES
-------
    518
    123