oracle 用逗号分隔带引号的字符串

oracle split by comma a quoted string

我发现了很多这样的问题,但其中 none 100% 适合我。我有 Oracle 11g Express

我有那个字符串

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

这意味着通常用逗号分隔数据,但也可以是空数据(甚至在一个系列中更多),如果数据中有逗号,则将其引用。

到目前为止最好的正则表达式是那个

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

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

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;  

提前致谢!

这似乎有效并处理 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>

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