我想根据某些字符串拆分下面的字符串。我们应该怎么做?
abc\\10.2.4\\string with spaces\\1.2.3-another-string\\def\\string_with_underscores
由'def'分割
您可以在以下查询中使用regexp_replace
:
with tab(str) as
(
select 'abc\\10.2.4\\string with spaces\\1.2.3-another-string\\def\\string_with_underscores'
from dual
)
select regexp_replace(str,'def.*','') str1,
regexp_replace(str,'.*def','') str2
from tab;
STR1 STR2
------------------------------------------------------- -------------------------
abc\\10.2.4\\string with spaces\\1.2.3-another-string\\ \\string_with_underscores
这会有什么好处吗?
SQL> with test (col) as
2 (select 'abc\\10.2.4\\string with spaces\\1.2.3-another-string\\def\\string_with_underscores' from dual)
3 select regexp_substr(replace(col, 'def', '#'), '[^#)]+', 1, level) res
4 from test
5 connect by level <= regexp_count(col, 'def') + 1;
RES
--------------------------------------------------------------------------------
abc\\10.2.4\\string with spaces\\1.2.3-another-string\\
\\string_with_underscores
SQL>
此选项不需要您事先知道您将获得多少个子串,它是动态的。