Updated on 2026-11-06 GMT+08:00

regexp_replace1

The regexp_replace1 function is used to replace the substring in source that matches pattern for the occurrence-th time with the specified string replace_string and return the result string.

The regexp_replace1 function is only applicable to Spark 2.4.5 or earlier.

Similar function: regexp_replace. The regexp_replace function has slightly different functionalities depending on the Spark version. Refer to regexp_replace for detailed functional descriptions.

Syntax

regexp_replace1(string , string  , string [, bigint ]) 

Parameters

Parameter

Mandatory

Type

Description

source

Yes

STRING

The string to be replaced.

pattern

Yes

STRING

A STRING type constant or regular expression. The pattern to be matched. For more regular expression writing specifications, see the regular expression specifications. An error is returned if pattern is an empty string.

replace_string

Yes

STRING

The string to replace the matched substring.

occurrence

No

BIGINT

Must be greater than or equal to 1. Indicates that the substring matching the occurrence-th time is replaced with replace_string. If 1, all matching substrings are replaced. If another type or less than 1, an error is returned. The default value is 1.

Return Values

Returns a value of type STRING.

  • When a non-existent group is referenced, no replacement is performed.
  • If replace_string is NULL and pattern has a match, NULL is returned.
  • If replace_string is NULL but pattern does not match, NULL is returned.
  • If source, pattern, or occurrence is NULL, NULL is returned.

Example Code

Returns 2222.

select regexp_replace1('abcd', '[a-z]', '2'); 

Returns 2bcd.

select regexp_replace1('abcd', '[a-z]', '2', 1); 

Returns a2cd.

select regexp_replace1('abcd', '[a-z]', '2', 2); 

Returns ab2d.

select regexp_replace1('abcd', '[a-z]', '2', 3); 

Returns abc2.

select regexp_replace1('abcd', '[a-z]', '2', 4);