命令格式:
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 用法