
# regexp_replace1
regexp_replace1函数用于将source字符串中第occurrence次匹配pattern的子串，替换成指定字符串replace_string后，返回结果字符串。
![](https://support.huaweicloud.com/sqlref-spark-dli/public_sys-resources/note_3.0-zh-cn.png)
regexp_replace1函数只适用于Spark 2.4.5及之前的版本。
相似函数：[regexp_replace](https://support.huaweicloud.com/sqlref-spark-dli/dli_spark_regexp_replace.html)，regexp_replace函数针对不同的Spark版本，功能略有差异，请参考[regexp_replace](https://support.huaweicloud.com/sqlref-spark-dli/dli_spark_regexp_replace.html)查看详细的功能说明。
#### 命令格式
```
regexp_replace1(string <source>, string <pattern>, string <replace_string>[, bigint <occurrence>])
```
#### 参数说明
表1参数说明 
| 参数             | 是否必选 | 参数类型   | 说明                                                                                   |
|:---|:---|:---|:---|
| source         | 是    | STRING | 待替换的字符                                                                               |
| pattern        | 是    | STRING | STRING类型常量或正则表达式。待匹配的模型。更多正则表达式编写规范，请参见正则表达式规范。pattern为空串时返回报错。                      |
| replace_string | 是    | STRING | 将匹配pattern的字符串替换后的字符串。                                                               |
| occurrence     | 否    | BIGINT | 必须大于等于1，表示将第occurrence次匹配的字符串替换为replace_string，为1时表示替换所有匹配的子串。为其他类型或小于1时，返回报错。默认值为1。 |
   
#### 返回值说明
返回STRING类型的值。
![](https://support.huaweicloud.com/sqlref-spark-dli/public_sys-resources/note_3.0-zh-cn.png)
- 当引用不存在的组时，不进行替换。
- 如果replace_string值为NULL且pattern有匹配，返回NULL。
- 如果replace_string值为NULL但pattern不匹配，返回NULL。
- source、pattern或occurrence值为NULL时，返回NULL。
 
#### 示例代码
返回 2222。
```
select regexp_replace1('abcd', '[a-z]', '2');
```
返回 2bcd。
```
select regexp_replace1('abcd', '[a-z]', '2', 1);
```
返回 a2cd。
```
select regexp_replace1('abcd', '[a-z]', '2', 2);
```
返回 ab2d。
```
select regexp_replace1('abcd', '[a-z]', '2', 3);
```
返回 abc2。
```
select regexp_replace1('abcd', '[a-z]', '2', 4);
```
