Sql Oracle正则表达式子字符串

Sql Oracle正则表达式子字符串,sql,regex,oracle,Sql,Regex,Oracle,我试图得到两个分号之间的值 select (REGEXP_SUBSTR(col,'[^:]+',1,2,null)) from test 以下是我正在处理的行: 1236:10:EXEC 1236::EXEC 在我的结果集中,我想得到: 10 <null> 您可以为此避免使用regexp;例如: select substr(col, instr(col, ':') +1, instr(col, ':', 1, 2) - instr(col, ':') -1) from test

我试图得到两个分号之间的值

select (REGEXP_SUBSTR(col,'[^:]+',1,2,null)) from test
以下是我正在处理的行:

1236:10:EXEC
1236::EXEC
在我的结果集中,我想得到:

10
<null>

您可以为此避免使用regexp;例如:

select substr(col, instr(col, ':') +1, instr(col, ':', 1, 2) - instr(col, ':') -1)
from test
工作原理:

select instr(col, ':') as firstColon,
       instr(col, ':', 1, 2) as secondColon,
       instr(col, ':', 1, 2) - instr(col, ':') -1 as lengthOfResultingString,
       substr(col, instr(col, ':') +1, instr(col, ':', 1, 2) - instr(col, ':') -1) as result
from test
给出:

FIRSTCOLON SECONDCOLON LENGTHOFRESULTINGSTRING RESULT
---------- ----------- ----------------------- ------------
         5           8                       2 10
         5           6                       0
这假定字符串中始终至少有两个冒号字符

使用regexp,您可以使用更慢但更紧凑的方式:

regexp_substr(col, ':([^:]*):', 1, 1, null, 1)

像这样使用regexp\u substr来处理空列表元素。第四个参数是您想要的列表中的元素:

with test (col) as
(
  select '1236:10:EX' from dual union all
  select '1543::Ex' from dual
)
select regexp_substr(col, '(.*?)(:|$)', 1, 2, NULL, 1) from test;

使用
REGEXP_SUBSTR,':([^:]*),1,1,null,1)
谢谢。那太好了
with test (col)  as
(
  select '1236:10:EX' from dual union all
  select '1543::Ex' from dual
)
select regexp_replace(col, '(:(.*):)|.', '\2') from test;
with test (col) as
(
  select '1236:10:EX' from dual union all
  select '1543::Ex' from dual
)
select regexp_substr(col, '(.*?)(:|$)', 1, 2, NULL, 1) from test;