2017-12-17 15 views
0

誰かが私を助けてください。コンマの位置に関係なく、引用符の間のカンマを削除するregexp_replaceを取得するために何日も試してきました。 例oracle regexp_replaceの間にカンマを削除するには

cold, gold, "Block 12C, Jones Avenue, Broad Street, London", car 

期待回答あなたは、二重引用符(REGEXP_SUBSTR)内のコンテンツを抽出コンマを交換し、使用して戻って古い文字列にそれを詰めることができ、事前

+0

文字列に最大で1組の倍音引用符がありますか? – miracle173

+0

クォートのペアが複数ある場合は、それらをネストすることはできますか? –

答えて

0

cold, gold, "Block 12C Jones Avenue Broad Street London", car 

感謝置き換えてください。これはまた、より効率的ではなく、読みにくいかもしれINSTRSUBSTRを使用して行うことができることを

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

注意。

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')); 

d E m O

0

が、私は望ましい結果を達成する単一の正規表現機能があることを疑います。 "明らかな"攻撃の仕方は、入力文字列を断片に分割し、必要に応じて二重引用符で囲まれた部分文字列からコンマを削除することです。 (それぞれの文字列に二重引用符が最大で1つのみ含まれていない限り、問題はより簡単な答えになりますが、サンプル入力文字列から判断すると、任意の数の二重引用符で囲まれた入力文字列に対して、

ここでは、再帰的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", 
関連する問題