只需快速检查,是否可以使用单个select语句替换所有匹配项。如果没有,我们计划编写一个函数来替换它。
示例字符串
select '#col1:val1@, #col2:val2@, #col3:val3@, #col4:val4@, #col5:val5@' str from dual
预期输出
str
---
col1 val1, col2 val2, col3 val3, col4 val4, col5 val5
到目前为止代码
with test_table as
(Select '#col1:val1@, #col2:val2@, #col3:val3@, #col4:val4@, #col5:val5@' str from dual)
select level "occurrence", REGEXP_substr(str, '#(.*?):', 1, level, 'in', 1) "column", REGEXP_substr(str, ':(.*?)@', 1, level, 'in', 1) "value"
from test_table
CONNECT BY REGEXP_COUNT(str, '#.+?:.+?@') >= level
其他详细信息
答案 0 :(得分:3)
<强>查询强>:
SELECT REGEXP_REPLACE(
'#col1:val1@, #col2:val2@, #col3:val3@, #col4:val4@, #col5:val5@',
'#(.*?):(.*?)@',
'\1 \2'
) AS str
FROM DUAL
<强>输出强>:
STR
-----------------------------------------------------
col1 val1, col2 val2, col3 val3, col4 val4, col5 val5
答案 1 :(得分:0)
我的尝试:
with test_table as
(Select '#col1:val1@, #col2:val2@, #col3:val3@, #col4:val4@, #col5:val5@' str
from dual)
select regexp_replace(regexp_replace(str, '[#@]', ''), '[ :]+', ' ') rez
from test_table
;
内部regexp_replace用#替换#和@(空字符串),外部regexp用一个空格替换:和空格。