0
1. <0,0><120.96,2000><241.92,4000><362.88,INF>
2. <0,0><143.64,2000><241.92,4000><362.88,INF>
3. <0,0><125.5,2000><241.92,4000><362.88,INF>
4. <0,0><127.5,2000><241.92,4000><362.88,INF>

以上是我在Oracle 10g 中的数据集。我需要如下输出

1. 120.96
2. 143.64
3. 125.5
4. 125.5

我想要的输出只是之前"comma" (120.96)。我尝试使用 REGEXP_SUBSTR 但我无法获得任何输出。如果有人能提供有效的方法来解决这个问题,那将非常有帮助

4

3 回答 3

0

这是一种方法,它首先解析出第二个元素,然后获取其中的第一个数字:

select regexp_substr(regexp_substr(x, '<[^>]*>', 1, 2), '[0-9.]+', 1, 1)

另一种方法只是获取字符串中的第三个数字:

select regexp_substr(x, '[0-9.]+', 1, 3)
于 2017-05-27T19:56:38.747 回答
0

这是一种不使用正则表达式的方法。查找“<”第二次出现的索引。然后找到第二次出现的“,”在子字符串中使用这些值。

    with
    data as
    (
    select '<0,0><120.96,2000><241.92,4000><362.88,INF>' x from dual
    UNION ALL
    select '<0,0><143.64,2000><241.92,4000><362.88,INF>' x from dual
    UNION ALL
    select '<0,0><125.5,2000><241.92,4000><362.88,INF>' from dual
    )
    select substr(x, instr(x,'<',1,2)+1, instr(x,',',1,2)- instr(x,'<',1,2)-1)
    from data

使用正则表达式的方法:识别第二次出现的数值,后跟逗号然后删除尾随逗号。

    with
    data as
    (
    select '<0,0><120.96,2000><241.92,4000><362.88,INF>' x from dual
    UNION ALL
    select '<0,0><143.64,2000><241.92,4000><362.88,INF>' x from dual
    UNION ALL
    select '<0,0><125.5,2000><241.92,4000><362.88,INF>' from dual
    )
    select 
    trim(TRAILING ',' FROM regexp_substr(x,'[0-9.]+,',1,2))
    from data
于 2017-05-28T18:38:28.357 回答
0

此示例使用 regexp_substr 获取包含在第二次出现小于号和逗号中的字符串:

SQL> with tbl(id, str) as (
     select 1, '<0,0><120.96,2000><241.92,4000><362.88,INF>' from dual union
     select 2, '<0,0><143.64,2000><241.92,4000><362.88,INF>' from dual union
     select 3, '<0,0><125.5,2000><241.92,4000><362.88,INF>'  from dual union
     select 4, '<0,0><127.5,2000><241.92,4000><362.88,INF>'  from dual
   )
   select id,
          regexp_substr(str, '<(.*?),', 1, 2, null, 1) value
   from tbl;

        ID VALUE
---------- -------------------------------------------
         1 120.96
         2 143.64
         3 125.5
         4 127.5

编辑:我意识到 OP 指定了 10g,而我给出的 regexp_substr 示例使用了 11g 中添加的第 6 个参数(子组)。这是一个使用 regexp_replace 的示例,它应该适用于 10g:

SQL> with tbl(id, str) as (
        select 1, '<0,0><120.96,2000><241.92,4000><362.88,INF>' from dual union
        select 2, '<0,0><143.64,2000><241.92,4000><362.88,INF>' from dual union
        select 3, '<0,0><125.5,2000><241.92,4000><362.88,INF>'  from dual union
        select 4, '<0,0><127.5,2000><241.92,4000><362.88,INF>'  from dual
      )
      select id,
             regexp_replace(str, '^(.*?)><(.*?),.*$', '\2') value
      from tbl;

        ID VALUE
---------- ----------
         1 120.96
         2 143.64
         3 125.5
         4 127.5

SQL>
于 2017-05-30T15:01:55.503 回答