が、私は望ましい結果を達成する単一の正規表現機能があることを疑います。 "明らかな"攻撃の仕方は、入力文字列を断片に分割し、必要に応じて二重引用符で囲まれた部分文字列からコンマを削除することです。 (それぞれの文字列に二重引用符が最大で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",
文字列に最大で1組の倍音引用符がありますか? – miracle173
クォートのペアが複数ある場合は、それらをネストすることはできますか? –