replace和regexp_replace替換字串
阿新 • • 發佈:2020-12-26
replace(字串,要被替換的字元,替換後的字元)
如果替換後的內容為空,則變成刪除作用了
replace(字串,要被替換的字元)
regexp_replace函式
REGEXP_REPLACE(source, pattern, replace_str)
source: 對應欄位
pattern: 正則表示式
replace_str:替換成什麼
regexp_replace函式是replace函式的擴充套件函式,用於通過正則表示式來進行匹配替換,預設情況下,每次匹配到的正則,都替換為replace_str,返回的字串與source字符集相同。如果source為非LOB型別,則返回varchar2資料型別,如果為LOB型別,則返回CLOB型別,該函式符合POSIX正則和Unicode正則。
- 替換字串
2.使用正則替換第一個字元是數字為a
select regexp_replace('123456788','^[0-9]','a') from dual
3.替換最後一個字元是字母的為9
select regexp_replace('12345678p','[a-zA-Z]$','9') from dual
4.把電話號碼+86 13856427896,變換成(+86)138-5642-7896
select regexp_replace('+86 13856427896','(\+[0-9]{2})( )([0-9]{3})([0-9]{4})([0-9]{4})','(\1)\3-\4-\5') as new_str from dual;
由此看來regexp_replace比replace功能更強大,我們可以用regexp_replace來替代replace