正则表达式-从字符串中提取数字

时间:2018-09-17 08:16:57

标签: sql oracle

我想在Oracle SQL中创建REGEX DEFINITION,这将返回一定数量的行,并且字符串中包含浮点数。该怎么做?

case1(n = 4: 48 -48h, 72 -72h, 96 -96h, 120 -120h)

n = 4>每个范围4条记录,检索到的值为:48.00,72.00,96.00 120.00

case2(n = 5: 0 -0h, 24.21 -24.21h, 47.77 -47.75h, 71.75 -71.75h, 96.08 -96.08h)

n = 5>每个范围5条记录,检索到的值为:00.00、24.21、47.77、71.75、96.08

输入:

select REGEXP_SUBSTR('(n=4: 48-48h, 72-72h, 96-96h, 120-120h)', MY_REGEX_DEFINITION ) as X from dual;

输出行:

X
48.00
72.00
96.00
120.00

1 个答案:

答案 0 :(得分:0)

我查找了一个有关如何分割字符串的示例,因为您首先需要这样做才能查找字符串中的所有数字。这是我对该示例的重做,以适合您的问题。不幸的是,它在最后返回一个空行,但是应该以它开头。

SELECT CAST(REGEXP_SUBSTR(result_row, '[^=]\d+\.?\d*' ) AS NUMBER)
FROM (
    SELECT 
        TRIM(REGEXP_SUBSTR('(n=5: 0-0h, 24.21-24.21h, 47.77-47.75h, 71.75-71.75h, 96.08-96.08h)', '[^ ]+h', 1, lines.column_value)) result_row
    FROM dual,
    TABLE (CAST (MULTISET
        (SELECT LEVEL FROM dual
            CONNECT BY instr('(n=5: 0-0h, 24.21-24.21h, 47.77-47.75h, 71.75-71.75h, 96.08-96.08h)', '-', 1, LEVEL - 1) > 0
        ) AS sys.odciNumberList ) ) lines
    ORDER BY  lines.column_value)//

This is the article我从中获取了示例代码。