# ReplaceRegExGroup - 指令碼與圖表函數

ReplaceRegExGroup() 會在取代輸入字串與指定複合規則運算式模式之間的一或多個相符項後傳回一個字串。取代相符文字的文字在 to\_str 引數中指定。該函數從左至右運作，但如果指定負 occurrence 值，則函數從右至左讀取。

此函數執行區分大小寫的 Regex 操作。您也可以使用變體 ReplaceRegExGroupI() 來執行不區分大小寫的 Regex 操作。

語法：

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`。第一組輸入文字和 Regex 模式之間有兩個相符項。但是，僅取代第二次出現的情況。 |
| `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 or https&gt;://&lt;domain&gt;.com/&lt;resource path&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 or https&gt;://&lt;domain&gt;.com/&lt;resource path&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-TW/sense/May2025/Subsystems/Hub/Content/Sense_Hub/Scripting/regular-expressions-qlik-analytics.htm)