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