【问题标题】:oracle split by comma a quoted stringoracle 用逗号分割一个带引号的字符串
【发布时间】:2016-09-16 10:31:22
【问题描述】:

我发现了很多这样的问题,但没有一个是 100% 适合我的。我有 oracle 11g express

我有那个字符串

'abcd,123,,defoifcd,"comma, in string",87765,,,hello'

这意味着通常用逗号分隔数据,但可以是空数据(甚至更多),如果数据中有逗号,则引用它。

目前为止最好的 reg exp 就是那个

'("[^"]*"|[^,]+)'

但这会将使用该查询的所有空数据放在末尾​​p>

with test as 
(select 
'abcd,123,,defoifcd,"comma, in string", 87765,,,hello'
str from dual  
)  
select REGEXP_SUBSTR(str, '("[^"]*"|[^,]+)', 1, rownum) split
from test  
connect by level <= length (regexp_replace (str, '("[^"]*"|[^,]+)'))  + 1;

我也尝试用 ,n/a 替换空数据,所以有

'abcd,123,n/a,defoifcd,"comma, in string",87765,n/a,n/a,hello'

但 regexp_replace 只替换第一次出现的空数据

select 
regexp_replace('abcd,123,,defoifcd,"comma, in string",87765,,,hello', '(,,)', ',n/a,')
str from dual;  

提前致谢!

【问题讨论】:

  • this 你已经看过了吗?那是使用 replace 而不是 regexp_replace 将虚拟值放入...
  • 是的,这种替换确实是个好主意

标签: oracle11g regexp-replace regexp-substr


【解决方案1】:

这似乎工作并处理 NULL:

SQL> with test as
    (select
    'abcd,123,,defoifcd,"comma, in string", 87765,,,hello'
    str from dual
    )
    select trim('"' from REGEXP_SUBSTR(str, '(".*?"|.*?)(,|$)', 1, level, NULL, 1)) split
    from test
    connect by level<=length(regexp_replace(str,'".*?"|[^,]*'))+1;

SPLIT
----------------------------------------------------
abcd
123

defoifcd
comma, in string
 87765


hello

9 rows selected.

SQL>

这篇文章为解决方案提供了动力:https://community.oracle.com/thread/528107?tstart=0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 2020-08-30
    • 2017-04-07
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多