如何在oracle pl / sql中拆分带有特殊字符的字符串

问题描述 投票:0回答:2

我想根据某些字符串拆分下面的字符串。我们应该怎么做?

abc\\10.2.4\\string with spaces\\1.2.3-another-string\\def\\string_with_underscores

由'def'分割

sql regex oracle oracle10g
2个回答
0
投票

您可以在以下查询中使用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

0
投票

这会有什么好处吗?

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>
  • 第1行和第2行代表您的数据
  • 第3 - 5行用于将该列拆分为行 第3行:REPLACE用'#'替换'def',后者成为新的分隔符 第5行:你将拥有与字符串+ 1中的'def'分隔符一样多的行(即如果有4个'def'分隔符,则结果将有5行)

此选项不需要您事先知道您将获得多少个子串,它是动态的。

© www.soinside.com 2019 - 2024. All rights reserved.