# ReplaceRegExGroup - 脚本和图表功能

ReplaceRegExGroup() 在替换输入字符串和指定的复合正则表达式模式之间的一个或多个匹配后，返回一个字符串。取代匹配文本的文本在 to\_str 参数中指定。函数从左到右运行，但如果指定一个负 occurrence 值，函数将从右到左读取。

该函数执行大小写敏感的正则表达式操作。您也可以用变种 ReplaceRegExGroupI() 来作为替代，执行不区分大小写的正则表达式操作。

语法：

ReplaceRegExGroup (text, regex, to\_str, group [, occurrence])

返回数据类型： 字符串

参数

| 参数 | 描述 |
| --- | --- |
| text | 输入字符串文本，您希望用 to\_str 中的新文本全部或部分替换该文本。 |
| regex | 定义何时替换文本的正则表达式。该参数与 text 参数之间的匹配将被替换。 |
| to\_str | 您要用来替换 text 中现有内容的新文本。 |
| group | 在复合正则表达式的情况下，组的编号。<br><br>如果正则表达式只包含一个组，请使用 ReplaceRegEx() 函数代替。替代方法是使用 ReplaceRegExGroup()，group 值为 `0`。<br><br>您可以指定一个负 group 值，从右向左搜索匹配项。 |
| occurrence | 要替换为新文本的匹配项（输入文本与正则表达式之间）的编号。<br><br>这是可选参数。如果省略，默认值为 0。当使用值为 0 或省略参数时，text 和 regex 之间的所有匹配结果都会被替换为 to\_str。<br><br>如果要从右向左识别匹配项，可以指定一个 occurrence 负值。 |

函数示例

| 示例 | 结果 |
| --- | --- |
| `ReplaceRegExGroup('abc123','([a-z])([0-9]+)','x',0)` | 返回 `abx`（替换完全匹配）。 |
| `ReplaceRegExGroup('abc123','([a-z])([0-9]+)','x',1)` | 返回 `abx123`（只替换匹配中的第一组）。 |
| `ReplaceRegExGroup('abc123','([a-z])([0-9]+)','x',2)` | 返回 `abcx`（只替换匹配中的第二组）。 |
| `ReplaceRegExGroup('abc123abc123','([a-z])([0-9]+)','x',1,2)` | 返回 `abc123abx123`。输入文本的第一组与正则表达式模式之间有两个匹配项。不过，只有第二次出现时才会被替换。 |
| `ReplaceRegExGroup('ABC123ABC123','([a-z])([0-9]+)','x',1)` | 返回 `ABC123ABC123`。换句话说，没有文本被替换。这是因为该函数区分大小写，且不使用该函数区分大小写的变体。 |
| `ReplaceRegExGroupI('ABC123ABC123','([a-z])([0-9]+)','x',1)` | 返回 `ABx123ABx123`。在本例中，由于使用了不区分大小写的函数变体 ReplaceRegExGroupI()，所以进行了文本替换。 |

## 适合使用它的情况

您可以使用 ReplaceRegExGroup() 对文本进行修改，以符合格式样式和合规标准，尤其是需要识别数据中的复杂样式时。该函数的附加参数 group 允许您只更新较大文本模式的某些部分。例如，您可以更新 URL 的特定部分。

如有需要，您还可以使用此函数屏蔽敏感信息，如个人身份信息 (PII)，使其不会显示给分析您应用程序的用户。

## 例 1 - 加载脚本以更新 URL 中的域名

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/StringFunctions/ReplaceRegExGroup.htm)

#### 加载脚本和结果

概述

打开数据加载编辑器，并将下面的加载脚本添加到新部分。

加载脚本包含：

* 一个最初包含输入字符串列表的表格 `URLs`，我们要在其中识别和更新特定的 URL。我们要更新这些 URL 中的域名。

* 创建一个字段，`ResourceURL_Updated` ，其中包含经过处理的 URL 的已修改的输入文本。

* 变量 `URL_RegEx`，用于存储正则表达式，以识别要处理的 URL。

我们的要求：

* 每个输入字符串包含两个 URL，第一个 URL 中指定了资源 URL。除列出的第一个 URL 外，我们不想更新任何其他 URL。

* 需要更新的 URL 格式为 &lt;https 或 https&gt; :// &lt;域&gt; .com/ &lt;资源路径&gt;。资源路径是可选项，但在本例中没有使用。

加载脚本

```
Set URL_RegEx = '(https?):\/\/(([a-zA-Z0-9]+)\.([a-zA-Z0-9]*)\.??([a-zA-Z0-9]*))\/?((([a-zA-Z0-9]*)*)((\/?([a-zA-Z0-9]*)*))*)\/{0,1}([\.,;]+(?=\s))*?';

URLs:

Load
ReplaceRegExGroup(URLsList, '$(URL_RegEx)', 'replacement-server', 2,1) as ResourceURL_Updated;

Load * Inline `
URLsList
The resource is located at https://testserver.com/files. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/files/worksheet. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/files/book. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/files/form. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/datamodel. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/resourcenew. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/page. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/page/overview. For more help, see: https://support.company.com.
` (delimiter is '\t');
```

结果

加载数据并打开工作表。创建新表并将该字段添加为维度：

* `ResourceURL_Updated`

结果表

| ResourceURL\_Updated |
| --- |
| The resource is located at https://replacement-server/datamodel. For more help, see: https://support.company.com. |
| The resource is located at https://replacement-server/files. For more help, see: https://support.company.com. |
| The resource is located at https://replacement-server/files/book. For more help, see: https://support.company.com. |
| The resource is located at https://replacement-server/files/form. For more help, see: https://support.company.com. |
| The resource is located at https://replacement-server/files/worksheet. For more help, see: https://support.company.com. |
| The resource is located at https://replacement-server/page. For more help, see: https://support.company.com. |
| The resource is located at https://replacement-server/page/overview. For more help, see: https://support.company.com. |
| The resource is located at https://replacement-server/resourcenew. For more help, see: https://support.company.com. |

## 例 2 - 更新 URL 中协议的图表表达式

[](https://help.qlik.com/en-US/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/StringFunctions/ReplaceRegExGroup.htm)

#### 图表表达式和结果

概述

打开数据加载编辑器，并将下面的加载脚本添加到新部分。

加载脚本包含：

* 一个包含输入字符串列表的表格 `URLsList`，我们要在其中识别和更新特定的 URL。我们希望将支持网站链接的协议从 http 更新为 https。

我们的要求：

* 每个输入字符串包含两个 URL，支持网站链接显示为第二个 URL。除列出的第二个 URL 外，我们不想更新任何其他 URL。

* 需要更新的 URL 格式为 &lt;https 或 https&gt; :// &lt;域&gt; .com/ &lt;资源路径&gt;。资源路径是可选项，但在本例中没有使用。

加载脚本

```
URLs:

Load * Inline `
URLsList
The resource is located at https://testserver.com/files. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/files/worksheet. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/files/book. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/files/form. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/datamodel. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/resourcenew. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/page. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/page/overview. For more help, see: http://support.company.com.
` (delimiter is '\t');
```

结果

加载数据并打开工作表。创建新表并将该字段添加为维度：

* `URLsList`

添加此计算维度：

```
=ReplaceRegExGroup(URLsList, '(https?):\/\/(([a-zA-Z0-9]+)\.([a-zA-Z0-9]*)\.??([a-zA-Z0-9]*))\/?((([a-zA-Z0-9]*)*)((\/?([a-zA-Z0-9]*)*))*)\/{0,1}([\.,;]+(?=\s))*?', 'https', 1,2)
```

结果表

| URLsList | =ReplaceRegExGroup(URLsList, '(https?):\/\/(([a-zA-Z0-9]+)\.([a-zA-Z0-9]\*)\.??([a-zA-Z0-9]\*))\/?((([a-zA-Z0-9]\*)\*)((\/?([a-zA-Z0-9]\*)\*))\*)\/{0,1}([\.,;]+(?=\s))\*?', 'https', 1,2) |
| --- | --- |
| The resource is located at https://testserver.com/files. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/datamodel. For more help, see: https://support.company.com. |
| The resource is located at https://testserver.com/files/worksheet. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/files. For more help, see: https://support.company.com. |
| The resource is located at https://testserver.com/files/book. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/files/book. For more help, see: https://support.company.com. |
| The resource is located at https://testserver.com/files/form. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/files/form. For more help, see: https://support.company.com. |
| The resource is located at https://testserver.com/datamodel. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/files/worksheet. For more help, see: https://support.company.com. |
| The resource is located at https://testserver.com/resourcenew. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/page. For more help, see: https://support.company.com. |
| The resource is located at https://testserver.com/page. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/page/overview. For more help, see: https://support.company.com. |
| The resource is located at https://testserver.com/page/overview. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/resourcenew. For more help, see: https://support.company.com. |

## 了解详情

* [在加载脚本和图表表达式中使用正则表达式](https://help.qlik.com/zh-CN/sense/November2025/Subsystems/Hub/Content/Sense_Hub/Scripting/regular-expressions-qlik-analytics.htm)