oracle 具有空值的特殊字符后的正则表达式值

vxqlmq5t  于 2022-11-03  发布在  Oracle
关注(0)|答案(2)|浏览(187)

我正在努力使用正则表达式将Spring拆分为Oracle数据库中列。

select (REGEXP_SUBSTR(replace('1:::9999', ' ',''), '[^: ]+', 1, 4)) from dual;

我需要从该字符串中获取第4个值作为列值,有时位置2、3的值为空,我的查询不起作用。我正在尝试找出什么正则表达式将起作用

k97glaaz

k97glaaz1#

您可以使用

select (REGEXP_SUBSTR(replace('1:::9999', ' ',''), '([^: ]*)(:|$)', 1, 4, 'i', 1)) from dual;

这里,([^: ]*)(:|$)匹配

  • ([^: ]*)-组1:除:和空格之外的任何零个或多个字符
  • (:|$)-组2,:或字符串结尾。
bbuxkriu

bbuxkriu2#

对于此任务,您不需要(较慢的)正则表达式,请使用简单的substr/instr函数:

with input_(val) as (
  select '1:::9999' from dual
  union all
  select '1:2::' from dual
  union all
  select '1:2::3:5' from dual
)
, replaced as (
  select input_.*, replace(val, ' ', '') as val_replaced
  from input_
)
select
  val,
  substr(
    val_replaced,
    /*Locate the first occurrence of a colon and get a substring ...*/
    instr(val_replaced, ':', 1, 3) + 1,
    /*.. until the end, if the next colon is absent, or until the next colon*/
    nvl(nullif(instr(val_replaced, ':', 1, 4), 0), length(val_replaced) + 1) - instr(val_replaced, ':', 1, 3) - 1
  ) as col
from replaced

| 瓦尔|列|
| - -|- -|
| 1:::9999|小行星9999|
| 1:2:|* 空值 *|
| 一比二比三比五|三个|
fiddle,但性能存在差异。

相关问题