如何使用 REGEXP_SUBSTR 解析数据?
我有一个这样的数据集(见下文),我尝试提取形式为 {variable_number_of_digits}{hyphen}{only_one_digit} 的数字:
I have a data set like this (see below) and I try to extract digits which are in form {variable_number_of_digits}{hyphen}{only_one_digit}:
with mcte as (
select 'ILLD/ELKJS/00000000/ELKJS/FHSH' as addr from dual
union all
select 'ILLD/EFECTE/0116988-7-002/ADFA/ADFG' as addr from dual
union all
select 'IIODK/1573230-0/2216755-7/' as addr from dual
union all
select 'IIODK/1573230-0/2216755-700/WRITE' as addr from dual
)
select addr,
REGEXP_SUBSTR(addr,'(/)([0-9-]+)',1,1,NULL,2) AS num1,
REGEXP_SUBSTR(addr,'(/)([^/]+/)([0-9-]+)',1,1,NULL,3) num2
from mcte
;
我没有得到正确的结果集,应该如下
I'm not getting a correct results set which should be the following
+-------------------------------------+-----------+-----------+
| ADDR | NUM1 | NUM2 |
+-------------------------------------+-----------+-----------+
| ILLD/ELKJS/00000000/ELKJS/FHSH | NULL | NULL |
| ILLD/EFECTE/0116988-7-002/ADFA/ADFG | NULL | NULL |
| IIODK/1573230-0/2216755-7/ | 1573230-0 | 2216755-7 |
| IIODK/1573230-0/2216755-700/WRITE | 1573230-0 | NULL |
+-------------------------------------+-----------+-----------+
如何实现这一目标?
推荐答案
如果你想从第二个和第三个 /
分隔的组中得到结果,那么:
If you want to get the results from the second and third /
delimited groups then:
with mcte ( addr ) as (
select 'ILLD/ELKJS/00000000/ELKJS/FHSH' from dual union all
select 'ILLD/EFECTE/0116988-7-002/ADFA/ADFG' from dual union all
select 'IIODK/1573230-0/2216755-7/' from dual union all
select 'IIODK/1573230-0/2216755-700/WRITE' from dual union all
select 'IIODK/TEST/1573230-0/2216755-700/WRITE' from dual
)
select addr,
REGEXP_SUBSTR(addr,'^[^/]*/(d+-d)/',1,1,NULL,1) AS num1,
REGEXP_SUBSTR(addr,'^[^/]*/[^/]*/(d+-d)/',1,1,NULL,1) num2
from mcte;
输出:
ADDR NUM1 NUM2
-------------------------------------- ------------------- -------------------
ILLD/ELKJS/00000000/ELKJS/FHSH
ILLD/EFECTE/0116988-7-002/ADFA/ADFG
IIODK/1573230-0/2216755-7/ 1573230-0 2216755-7
IIODK/1573230-0/2216755-700/WRITE 1573230-0
IIODK/TEST/1573230-0/2216755-700/WRITE 1573230-0
更新:
如果您只想匹配第一个和第二个模式而不关心它们在字符串中的位置,那么:
If you just want the first and second pattern that match and do not care where they are in the string then:
with mcte ( addr ) as (
select 'ILLD/ELKJS/00000000/ELKJS/FHSH' from dual union all
select 'ILLD/EFECTE/0116988-7-002/ADFA/ADFG' from dual union all
select 'IIODK/1573230-0/2216755-7/' from dual union all
select 'IIODK/1573230-0/2216755-700/WRITE' from dual union all
select 'IIODK/TEST/1573230-0/2216755-700/WRITE' from dual union all
select '1234567-8' from dual union all
select '1234567-8/9876543-2' from dual union all
select '1234567-8/TEST/9876543-2' from dual
)
select addr,
REGEXP_SUBSTR(addr,'(^|/)(d+-d)(/|$)',1,1,NULL,2) AS num1,
REGEXP_SUBSTR(addr,'(^|/)d+-d(/.+?)?/(d+-d)(/|$)',1,1,NULL,3) num2
from mcte;
输出:
ADDR NUM1 NUM2
-------------------------------------- ------------------- ------------------
ILLD/ELKJS/00000000/ELKJS/FHSH
ILLD/EFECTE/0116988-7-002/ADFA/ADFG
IIODK/1573230-0/2216755-7/ 1573230-0 2216755-7
IIODK/1573230-0/2216755-700/WRITE 1573230-0
IIODK/TEST/1573230-0/2216755-700/WRITE 1573230-0
1234567-8 1234567-8
1234567-8/9876543-2 1234567-8 9876543-2
1234567-8/TEST/9876543-2 1234567-8 9876543-2
相关文章