繁体   English   中英

在双引号之间替换字符串中的字符

[英]Replacing characters in a string when between double quotes

我想在动态位置之间(即双引号之间)替换我的字符串中的逗号。 请注意,如果重要的话,我的字符串中出现的双引号不会超过 2 次。

我的例子:

'randomtext,123,"JEAN SEBASTIEN, GUY, DANIEL",sun'

期望的输出:

'randomtext,123,"JEAN SEBASTIEN GUY DANIEL",sun'

到目前为止,我已经尝试过将REGEXP_REPLACE()INSTR()混合使用,但无法完成任何操作。

干杯

短而干净。

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 |
+------------------------------------------------+

SQL小提琴

此外 -
简短而干净的通用版本

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" |
+------------------------------------------------------------------------------------------------------------+

SQL小提琴

假设您正在处理 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

哪些输出:

\n价值 |  REPLACED_VALUE                                \n :--------------------------------------------------------- |  :---------------------------------------------\n randomtext,123,"JEAN SEBASTIEN, GUY, DANIEL",sun |  randomtext,123,"JEAN SEBASTIEN GUY DANIEL",sun\n randomtext,123,"A,""BC"", D",sun |  randomtext,123,"A ""BC"" D",sun               \n

db<> 在这里摆弄


更新

如果您需要处理字符串中的多个带引号的术语(带有嵌套引号):

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;

哪些输出:

\n价值 |  REPLACED_VALUE                                \n :--------------------------------------------------------- |  :---------------------------------------------\n L,M,N | 长、中、短                                         \n randomtext,123,"JEAN SEBASTIEN, GUY, DANIEL",sun |  randomtext,123,"JEAN SEBASTIEN GUY DANIEL",sun\n randomtext,123,"A,""BC"", D",sun |  randomtext,123,"A ""BC"" D",sun               \n E,"F, G",H,"I,""J""", K |  E,"FG",H,"I""J""", K                        \n

db<> 在这里摆弄

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM