select
substr(code, 1, 1) -- first character
||
'0' -- the '0' at the second position
||
substr(code, 3) -- everything from the third character to the end
from codes;
with demo (str) as
( select '11111' from dual )
select str as original
, regexp_replace(str, '^(.{1})(.{1})(.*)$', '\10\3') as replaced
from demo;
ORIGINAL REPLACED
-------- ---------
11111 10111
with demo (str) as
( select 'Sneeze' from dual )
select str as original
, regexp_replace(str, '^(.{2})(.{2})(.*)$', '\1oo\3') as replaced
from demo;
ORIGINAL REPLACED
-------- ---------
Sneeze Snooze
2条答案
按热度按时间cclgggtu1#
您正在存储一个字符串,其中要用字符“0”替换左数第二个字符。您可以使用SUBSTR来实现这一点。
字符串
4urapxun2#
substr
和串联可能最适合这样的简单情况,但您也可以查看regex_replace
:个字符
\1
和\3
是指圆括号中的第1个和第3个表达式。这里并不需要
{1}
长度说明符,只是在需要调整要替换的字符的位置和长度时才使用。例如,替换两个字符的子字符串可能如下所示:型
小提琴:https://dbfiddle.uk/x7c7P2SX
Regex细分:https://regex101.com/r/uLCiLu/1的