我想替换字符串中动态位置之间(即双引号之间)的逗号。请注意,如果这很重要,那么我的字符串中不会出现超过两次的双引号。
我的示例:
'randomtext,123,"JEAN SEBASTIEN, GUY, DANIEL",sun'
所需的输出:
'randomtext,123,"JEAN SEBASTIEN GUY DANIEL",sun'
到目前为止,我已经尝试将REGEXP_REPLACE()
与INSTR()
混合使用,但无法完成任何工作。
欢呼
答案 0 :(得分:3)
短而干净。
with t(str) as (select 'randomtext,123,"JEAN SEBASTIEN, GUY, DANIEL",sun' from dual)
select regexp_replace(str,'(^[^"]*|[^"]*$)|,','\1') as result
from t
-
+------------------------------------------------+
| RESULT |
+------------------------------------------------+
| randomtext,123,"JEAN SEBASTIEN GUY DANIEL",sun |
+------------------------------------------------+
此外-
简短的通用版本
with t(str) as
(
select 'Well,you,went,uptown,riding,in,your,limousine' from dual
union all select 'With,your,fine,"Park, Avenue, clothes"' from dual
union all select 'You,had,the,"Dom, Perignon",in,your,hand,"And, the, spoon",up,your,nose' from dual
union all select '"And, when, you",wake,"up, in, the, morning"' from dual
union all select '"With, your, head, on, fire"' from dual
union all select '"And",your,"eyes, too, bloody","to, see",Go,"on, and, cry, in",your,coffee,"But","don''t","come, bitchin''","to, me"' from dual
)
select regexp_replace(str, '((^|").*?("|$))|,', '\1') as result
from t
-
+------------------------------------------------------------------------------------------------------------+
| RESULT |
+------------------------------------------------------------------------------------------------------------+
| Well,you,went,uptown,riding,in,your,limousine |
| With,your,fine,"Park Avenue clothes" |
| You,had,the,"Dom Perignon",in,your,hand,"And the spoon",up,your,nose |
| "And when you",wake,"up in the morning" |
| "With your head on fire" |
| "And",your,"eyes too bloody","to see",Go,"on and cry in",your,coffee,"But","don't","come bitchin'","to me" |
+------------------------------------------------------------------------------------------------------------+
答案 1 :(得分:3)
假设您正在使用CSV,那么根据此示例数据,您可能还会嵌套双引号:
CREATE TABLE test_data ( value ) AS
SELECT 'randomtext,123,"JEAN SEBASTIEN, GUY, DANIEL",sun' FROM DUAL UNION ALL
SELECT 'randomtext,123,"A, ""BC"", D",sun' FROM DUAL;
您可以使用正则表达式^(.*?)("([^\"]|\\")+")(.*)$
来匹配引号之前,引号内和之后的术语,然后仅在中间部分替换逗号:
SELECT value,
REGEXP_SUBSTR( value, '^(.*?)("([^\"]|"")+")(.*)$', 1, 1, NULL, 1 )
|| REPLACE(
REGEXP_SUBSTR( value, '^(.*?)("([^\"]|"")+")(.*)$', 1, 1, NULL, 2 ),
','
)
|| REGEXP_SUBSTR( value, '^(.*?)("([^\"]|"")+")(.*)$', 1, 1, NULL, 4 ) replaced_value
FROM test_data
哪个输出:
VALUE | REPLACED_VALUE :----------------------------------------------- | :--------------------------------------------- randomtext,123,"JEAN SEBASTIEN, GUY, DANIEL",sun | randomtext,123,"JEAN SEBASTIEN GUY DANIEL",sun randomtext,123,"A, ""BC"", D",sun | randomtext,123,"A ""BC"" D",sun
db <>提琴here
如果您需要处理字符串中多个带引号的术语(带嵌套引号):
CREATE TABLE test_data ( value ) AS
SELECT 'randomtext,123,"JEAN SEBASTIEN, GUY, DANIEL",sun' FROM DUAL UNION ALL
SELECT 'randomtext,123,"A, ""BC"", D",sun' FROM DUAL UNION ALL
SELECT 'E,"F, G",H,"I, ""J""", K' FROM DUAL UNION ALL
SELECT 'L,M,N' FROM DUAL;
然后,您可以使用递归子查询分解子句:
WITH replacements( value, prefix, suffix ) AS (
SELECT value,
REGEXP_SUBSTR( value, '^(.*?)("([^\"]|"")+"|$)(.*)$', 1, 1, NULL, 1 )
|| REPLACE(
REGEXP_SUBSTR( value, '^(.*?)("([^\"]|"")+"|$)(.*)$', 1, 1, NULL, 2 ),
','
),
REGEXP_SUBSTR( value, '^(.*?)("([^\"]|"")+"|$)(.*)$', 1, 1, NULL, 4 )
FROM test_data
UNION ALL
SELECT value,
prefix
|| REGEXP_SUBSTR( suffix, '^(.*?)("([^\"]|"")+"|$)(.*)$', 1, 1, NULL, 1 )
|| REPLACE(
REGEXP_SUBSTR( suffix, '^(.*?)("([^\"]|"")+"|$)(.*)$', 1, 1, NULL, 2 ),
','
),
REGEXP_SUBSTR( suffix, '^(.*?)("([^\"]|"")+"|$)(.*)$', 1, 1, NULL, 4 )
FROM replacements
WHERE suffix IS NOT NULL
)
SELECT value,
prefix AS replaced_value
FROM replacements
WHERE suffix IS NULL;
哪个输出:
VALUE | REPLACED_VALUE :----------------------------------------------- | :--------------------------------------------- L,M,N | L,M,N randomtext,123,"JEAN SEBASTIEN, GUY, DANIEL",sun | randomtext,123,"JEAN SEBASTIEN GUY DANIEL",sun randomtext,123,"A, ""BC"", D",sun | randomtext,123,"A ""BC"" D",sun E,"F, G",H,"I, ""J""", K | E,"F G",H,"I ""J""", K
db <>提琴here