阿里云SQL - regexp_replace

命令格式:
    regexp_replace(source, pattern, replace_string, occurrence)
返回值: 
    将source字符串中匹配pattern的子串替换成指定字符串后返回,当输入source, pattern, occurrence参数为NULL时返回NULL,若replace_string为NULL且pattern有匹配,返回NULL,replace_string为NULL但pattern不匹配,则返回原串。
参数说明: 
●  source: string类型,要替换的字符串。
●  pattern: string类型常量,要匹配的模式,pattern为空串时抛异常。
●  replace_string:string,将匹配的pattern替换成的字符串。
●  occurrence: bigint类型常量,必须大于等于0,表示将第几次匹配替换成replace_string,为0时表示替换掉所有的匹配子串。其它类型或小于0抛异常。
●  当引用不存在的组时,不进行替换。

例如:
regexp_replace(logtime,'-',' ')

regexp_replace("123.456.7890","([[:digit:]]{3})\\.([[:digit:]]{3})\\.([[:digit:]]{4})","(\\1)\\2-\\3",0) 结果为(123)456-7890

regexp_replace("abcd","(.)","\\1 ",0) 结果为"a b c d "

regexp_replace("abcd","(.)","\\1 ",1) 结果为"a bcd"

regexp_replace("abcd","(.)","\\2",1) 结果为"abcd",因为pattern中只定义了一个组,引用的第二个组不存在。

regexp_replace("abcd","(.*)(.)$","\\2",0) 结果为"d"

regexp_replace("abcd","a","\\1",0),结果为” \1bcd”,因为在pattern中没有组的定义,所以\1直接输出为字符。

注:阿里云也支持 replace 用法

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值