你不需要正则表达式;您可以使用INSTR、SUBSTR 和REPLACE 来满足您的需求:
with test(s) as (
select 'Heyho || HeyheyHo' from dual
)
/* the query */
select s as input,
substr(s, 1, instr(s, '||')+1) ||
replace( substr(s, instr(s, '||')+2), 'y', 'i') as result
from test
给予:
INPUT RESULT
----------------- --------------------
Heyho || HeyheyHo Heyho || HeiheiHo
它是如何工作的:
select s as input,
substr(s, 1, instr(s, '||')+1) beforeDelimiter,
substr(s, instr(s, '||')+2) afterDelimiter,
replace( substr(s, instr(s, '||')+2), 'y', 'i') afterDelimiterEdited,
substr(s, 1, instr(s, '||')+1) ||
replace( substr(s, instr(s, '||')+2), 'y', 'i') as result
from test
给予:
INPUT BEFOREDELI AFTERDELIM AFTERDELIM RESULT
----------------- ---------- ---------- ---------- --------------------
Heyho || HeyheyHo Heyho || HeyheyHo HeiheiHo Heyho || HeiheiHo
如果字符串中出现多个||,replace 将在第一次出现后修改字符。
根据 Mathguy 的评论,我不能说这个解决方案比正则表达式更快。
正则表达式的解决方案可能是:
select regexp_replace(s, 'y', 'i', instr(s, '||') ) as result
以下是对使用相同数据(500 万行)以相同方式创建的 2 个表进行的小型性能测试:
SQL> create table testA3(s) as
2 select regexp_replace(s, 'y', 'i', instr(s, '||') ) as result
3 from testA;
Table created.
Elapsed: 00:00:30.75
SQL> create table testB3(s) as
2 select substr(s, 1, instr(s, '||')+1) ||
3 replace( substr(s, instr(s, '||')+2), 'y', 'i') as result
4 from testB;
Table created.
Elapsed: 00:00:14.82
这里的标准方法似乎更快;使用 3M 行的相同测试,正则表达式方法耗时 18 秒,标准方法耗时 7 秒。
测试当然不是详尽无遗的,结果可能会因许多事情而改变,但即使在这种情况下,也需要考虑标准方式作为正则表达式的一个很好的替代方法,在这种情况下需要许多标准操作才能获得相同的结果正则表达式的结果。
这是 3M 行的完整测试;我做了一个CREATE 和 2 个INSERTs 以避免CONNECT BY 的内存问题,级别非常高。
此外,在 3M 和 5M 行测试之间,我删除了表并再次创建它们,以确保缓存不会影响结果。
SQL> create table testA(s) as
2 select 'Heyho || HeyheyHo' || level || 'HeyheyHo'
3 from dual
4 connect by level <= 1000000;
Table created.
SQL> create table testB(s) as
2 select 'Heyho || HeyheyHo' || level || 'HeyheyHo'
3 from dual
4 connect by level <= 1000000;
Table created.
SQL> insert into testB(s)
2 select 'Heyho || HeyheyHo' || to_char(level + 1000000) || 'HeyheyHo'
3 from dual
4 connect by level <= 1000000;
1000000 rows created.
SQL> insert into testA(s)
2 select 'Heyho || HeyheyHo' || to_char(level + 1000000) || 'HeyheyHo'
3 from dual
4 connect by level <= 1000000;
1000000 rows created.
SQL> insert into testB(s)
2 select 'Heyho || HeyheyHo' || to_char(level + 2000000) || 'HeyheyHo'
3 from dual
4 connect by level <= 1000000;
1000000 rows created.
SQL> insert into testA(s)
2 select 'Heyho || HeyheyHo' || to_char(level + 2000000) || 'HeyheyHo'
3 from dual
4 connect by level <= 1000000;
1000000 rows created.
SQL> select count(1), count(distinct s) from testA;
COUNT(1) COUNT(DISTINCTS)
---------- ----------------
3000000 3000000
SQL> select count(1), count(distinct s) from testB;
COUNT(1) COUNT(DISTINCTS)
---------- ----------------
3000000 3000000
SQL> set timing on
SQL> create table testA2(s) as
2 select regexp_replace(s, 'y', 'i', instr(s, '||')+2 ) as result
3 from testA;
Table created.
Elapsed: 00:00:17.66
SQL> create table testB2(s) as
2 select substr(s, 1, instr(s, '||')+1) ||
3 replace( substr(s, instr(s, '||')+2), 'y', 'i') as result
4 from testB;
Table created.
Elapsed: 00:00:06.96
SQL>