

 从补丁 198 开始，Amazon Redshift 将不再支持创建新的 Python UDF。现有的 Python UDF 将继续正常运行至 2026 年 6 月 30 日。有关更多信息，请参阅[博客文章](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

# REGEXP\$1REPLACE 函数
<a name="REGEXP_REPLACE"></a>

在字符串中搜索正则表达式模式并将该模式的每个匹配项替换为指定字符串。REGEXP\$1REPLACE 与 [REPLACE 函数](r_REPLACE.md)相似，只不过前者可让您在字符串中搜索正则表达式模式。有关正则表达式的更多信息，请参阅 [POSIX 运算符](pattern-matching-conditions-posix.md)和 Wikipedia 中的 [Regular expression](https://en.wikipedia.org/wiki/Regular_expression)。

REGEXP\$1REPLACE 与 [TRANSLATE 函数](r_TRANSLATE.md)和 [REPLACE 函数](r_REPLACE.md)相似，只不过 TRANSLATE 进行多次单字符替换，REPLACE 一次性将整个字符串替换为其他字符串，而 REGEXP\$1REPLACE 可让您在字符串中搜索正则表达式模式。

## 语法
<a name="REGEXP_REPLACE-synopsis"></a>

```
REGEXP_REPLACE( source_string, pattern [, replace_string [ , position [, parameters ] ] ] )
```

## 参数
<a name="REGEXP_REPLACE-arguments"></a>

 *source\$1string*   
要搜索的 `CHAR` 或 `VARCHAR` 字符串表达式（如列名称）。

 * 模式*   
表示正则表达式模式的 UTF-8 字符串文本。有关更多信息，请参阅 [POSIX 运算符](pattern-matching-conditions-posix.md)。

*replace\$1string*  
（可选）将替换模式的每次出现的 `CHAR` 或 `VARCHAR` 字符串表达式（如列名称）。默认值是空字符串 ("")。

 *position*   
（可选）指示在 *source\$1string* 中开始搜索的位置的正整数。此位置基于字符数而不是字节数，这是为了将多字节字符作为单字符计数。默认值为 `1`。如果 *position* 小于 `1`，则搜索从 *source\$1string* 的第一个字符开始。如果 *position* 大于 *source\$1string* 中的字符数量，则结果为 *source\$1string*。

 *参数*   
（可选）一个或多个字符串文本，指示函数与模式的匹配方式。可能的值包括：  
+ c – 执行区分大小写的匹配。默认情况下，使用区分大小写的匹配。
+ i – 执行不区分大小写的匹配。
+ p – 使用 Perl 兼容正则表达式 (PCRE) 方言解释模式。有关 PCRE 的更多信息，请参阅 Wikipedia 中的 [Perl Compatible Regular Expressions](https://en.wikipedia.org/wiki/Perl_Compatible_Regular_Expressions)。

## 返回类型
<a name="REGEXP_REPLACE-return-type"></a>

VARCHAR

如果 *pattern* 或 *replace\$1string* 为 `NULL`，则返回 `NULL`。

## 示例
<a name="REGEXP_REPLACE-examples"></a>

要使用不区分大小写的匹配替换字符串 `FOX` 在值 `quick brown fox` 内的所有出现，请使用以下示例。

```
SELECT REGEXP_REPLACE('the fox', 'FOX', 'quick brown fox', 1, 'i');

+---------------------+
|   regexp_replace    |
+---------------------+
| the quick brown fox |
+---------------------+
```

以下示例使用用 PCRE 方言编写的模式来定位至少包含一个数字和一个小写字母的单词。它使用 `?=` 运算符，它在 PCRE 中具有特定的前瞻含义。要将此单词的每次出现替换为值 `[hidden]`，请使用以下示例。

```
SELECT REGEXP_REPLACE('passwd7 plain A1234 a1234', '(?=[^ ]*[a-z])(?=[^ ]*[0-9])[^ ]+', '[hidden]', 1, 'p');

+-------------------------------+
|        regexp_replace         |
+-------------------------------+
| [hidden] plain A1234 [hidden] |
+-------------------------------+
```

以下示例使用用 PCRE 方言编写的模式来定位至少包含一个数字和一个小写字母的单词。它使用 `?=` 运算符，它在 PCRE 中具有特定的前瞻含义。要将此单词的每次出现替换为值 `[hidden]`，但与前面的示例不同，它使用不区分大小写的匹配，请使用以下示例。

```
SELECT REGEXP_REPLACE('passwd7 plain A1234 a1234', '(?=[^ ]*[a-z])(?=[^ ]*[0-9])[^ ]+', '[hidden]', 1, 'ip');

+----------------------------------+
|          regexp_replace          |
+----------------------------------+
| [hidden] plain [hidden] [hidden] |
+----------------------------------+
```

以下示例使用 TICKIT 示例数据库的 USERS 表中的数据。有关更多信息，请参阅 [示例数据库](c_sampledb.md)。

要删除电子邮件地址中的 `@` 和域名，请使用以下示例。

```
SELECT email, REGEXP_REPLACE(email, '@.*\\.(org|gov|com|edu|ca)$')
FROM users
ORDER BY userid LIMIT 4;

+-----------------------------------------------+-----------------------+
|                     email                     |    regexp_replace     |
+-----------------------------------------------+-----------------------+
| Etiam.laoreet.libero@sodalesMaurisblandit.edu | Etiam.laoreet.libero  |
| Suspendisse.tristique@nonnisiAenean.edu       | Suspendisse.tristique |
| amet.faucibus.ut@condimentumegetvolutpat.ca   | amet.faucibus.ut      |
| sed@lacusUtnec.ca                             | sed                   |
+-----------------------------------------------+-----------------------+
```

要使用 `internal.company.com` 替换电子邮件地址的域名，请使用以下示例。

```
SELECT email, REGEXP_REPLACE(email, '@.*\\.[[:alpha:]]{2,3}','@internal.company.com') 
FROM users
ORDER BY userid LIMIT 4;

+-----------------------------------------------+--------------------------------------------+
|                     email                     |               regexp_replace               |
+-----------------------------------------------+--------------------------------------------+
| Etiam.laoreet.libero@sodalesMaurisblandit.edu | Etiam.laoreet.libero@internal.company.com  |
| Suspendisse.tristique@nonnisiAenean.edu       | Suspendisse.tristique@internal.company.com |
| amet.faucibus.ut@condimentumegetvolutpat.ca   | amet.faucibus.ut@internal.company.com      |
| sed@lacusUtnec.ca                             | sed@internal.company.com                   |
+-----------------------------------------------+--------------------------------------------+
```