【发布时间】:2016-06-14 10:00:38
【问题描述】:
只是快速检查一下,是否可以用单个 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
其他详情
- 字符串模式是固定的并且事先已知。
- 给定模式的出现是动态的,可能有任意数量的 [column:value] 对 - 它们实际上是 [table column:alias] 集。
- 为了简单起见,我已经修剪了示例字符串,但是它包含其他详细信息(表连接、where 子句等),因此我们需要替换所有出现的地方。这实际上是一个 select 语句。
【问题讨论】:
标签: regex string oracle replace