Regex 在Oracle中使用正则表达式按分隔符拆分字符串
我需要在Oracle中编写脚本,将查询中的字符串“BP/593/00294”和“NC//12345”按/字符拆分,以便在单独的列中包含值 我的想法是:Regex 在Oracle中使用正则表达式按分隔符拆分字符串,regex,oracle,plsql,Regex,Oracle,Plsql,我需要在Oracle中编写脚本,将查询中的字符串“BP/593/00294”和“NC//12345”按/字符拆分,以便在单独的列中包含值 我的想法是: select regexp_substr(mystr, '[^/]+') as col1, regexp_substr(c.usertext21,'[^/]+',1,2) as col2 from mytable 但在col2中,我从第二个字符串中释放了空字符串值。 我需要保留两个字符串中的每个值。结果应该是: 可乐 可乐 可乐 英国石油
select regexp_substr(mystr, '[^/]+') as col1,
regexp_substr(c.usertext21,'[^/]+',1,2) as col2
from mytable
但在col2中,我从第二个字符串中释放了空字符串值。
我需要保留两个字符串中的每个值。结果应该是:
可乐
可乐
可乐
英国石油公司
593
00294
数控
12345
您可以在字符串开头或/
之后捕获0个或更多字符,而不是/
:
select
regexp_substr('BP/593/00294', '(^|/)([^/]*)') as col1,
regexp_substr('BP/593/00294', '(^|/)([^/]*)', 1, 2, null, 2) as col2,
regexp_substr('BP/593/00294', '(^|/)([^/]*)', 1, 3, null, 2) as col3
from dual
看
详细信息
-捕获组1:字符串的开头或(^ |/)
/
-捕获组2:除([^/]*)
之外的任何0个或更多字符/
请注意提取组2值的
2
参数。请参阅。如果要捕获的拆分数目未知,并且希望每个拆分都是一个单独的行,则可以使用connect by子句:
with example as (
select 'NC//12345/12qwe/////12sadf' text from dual
)
select regexp_substr(e.text, '[^/]+', 1, LEVEL) col
from example e
connect by regexp_instr(e.text, '[/]+', 1, LEVEL - 1) > 0
非常有魅力!非常感谢你!