oracle regexp_replace删除Quote之间的逗号

时间:2017-12-17 00:21:47

标签: oracle oracle11g sqlplus

请有人帮帮我。无论逗号位置如何,已经尝试了几天来获取regexp_replace以删除引号之间的逗号。 实施例

option.rect

预期答案

elideText()

提前致谢

3 个答案:

答案 0 :(得分:0)

您可以在双引号(REGEXP_SUBSTR)中提取内容,替换逗号,然后使用替换将其填充回旧字符串。

select REPLACE (whole_str,quoted_str,REPLACE (quoted_str,',')) FROM
(                
select 
    whole_str, 
    REGEXP_SUBSTR( whole_str, '^[^"]*("[^"]+")',1,1,NULL,1) quoted_str
  FROM yourtable
  );

DEMO

请注意,这也可以使用INSTRSUBSTR来完成,这可能更有效,但难以阅读。

答案 1 :(得分:0)

您可以使用regexp_replace获取所需的输出:

with t( id , val ) as(
select 1,'cold, gold, "Block 12C, Jones Avenue, Broad Street, London", car' from dual union
select 2,'"Block 12C, Jones Avenue, Broad Street, London", car, cold, gold' from dual )
select id, val value
  from t
 model dimension by( id ) measures( val )
 rules iterate(100)( val[any] = regexp_replace(val[cv()],',(([^"]*"){2})*([^"]*"[^"]*)$',' \1\3') );

docs

答案 2 :(得分:0)

我怀疑是否有一个正则表达式函数可以实现所需的结果。 “明显的”攻击线是将输入字符串分成几部分,并根据需要从每个双引号子字符串中删除逗号。 (除非每个字符串最多有一个双引号子字符串,在这种情况下问题有更简单的答案 - 但从样本输入字符串判断,对于具有任意数量的双精度的输入字符串,可能需要进行相同的操作。引用子串。)

以下是使用递归WITH子句的解决方案 - 因此它需要Oracle 11.2或更高版本。 (对于早期版本,可以使用具有CONNECT BY分层查询的解决方案。)我使用正则表达式按要求编写它;如果速度成为一个问题,它可以用标准的INSTR,SUBSTR和REPLACE函数重写。

在第一个因子子查询(WITH子句中的子查询)中,我创建了一些输入,以测试解决方案是否在不同情况下返回正确的结果。

with
  inputs ( str ) as (
    select 'cold, gold, "Block 12C, Jones Ave., London", car' from dual union all
    select '"One, two, three","Four, five six,",'             from dual union all
    select 'No, double-quotes, in this, string'               from dual union all
    select 'No commas in "double quotes" here'                from dual
  ),
  r ( str, init, quoted, fin ) as (
    select  str, null, null, str
      from  inputs
    union all
    select  str, 
            init || replace(quoted, ',') || regexp_substr(fin, '[^"]*'),
            regexp_substr(fin, '"[^"]*"'),
            regexp_substr(fin, '([^"]*"){2}(.*)', 1, 1, null, 2)
      from  r
      where quoted is not null or fin is not null
  )
select str, init as new_str
from   r 
where  quoted is null and fin is null
;

STR                                           NEW_STR
--------------------------------------------- -------------------------------------------
No, double-quotes, in this, string            No, double-quotes, in this, string
cold, gold, "Block 12C, Jay Ave, London", car cold, gold, "Block 12C Jay Ave London", car
No commas in "double quotes" here             No commas in "double quotes" here
"One, two, three","Four, five six,",          "One two three","Four five six",
相关问题