# IndexRegExGroup - 指令碼與圖表函數

IndexRegExGroup() 會搜尋輸入字串並傳回指定複合規則運算式模式第 n 次出現的起始位置。可選的第四個引數 count 會提供 n 的值，如果省略則為 1。字串中的位置編號是由左至右從 1 算起。如果未找到相符項，則該函數會傳回 0。

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

語法：

IndexRegExGroup (text, regex, group [, count])

傳回的資料類型： 整數

引數

| 引數 | 描述 |
| --- | --- |
| text | 要在其中搜尋規則運算式的輸入字串文字。 |
| regex | 用於搜尋輸入字串的規則運算式。 |
| group | 在複合規則運算式的情況下，群組的編號。<br><br>group 的 `0` 值傳回整個 Regex 的索引。但是，如果規則運算式只需要傳回整個相符項的索引，請改用 IndexRegEx() 函數。<br><br>您可以指定負 group 值以從右到左搜尋相符項。 |
| count | 相符項的編號。當可能在文字中找到規則運算式的多個相符項時，這非常有用。例如，指定值 `4` 以搜尋第四個相符項的位置。<br><br>此為選用引數。如果未指定，則預設為 1。您可以指定負值以從右到左搜尋相符項。 |

函數範例

| 範例 | 結果 |
| --- | --- |
| `IndexRegExGroup('abc123','([a-z])([0-9]+)',0)` | 傳回 `3` (完整規則運算式的起始位置)。 |
| `IndexRegExGroup('abc123','([a-z])([0-9]+)',1)` | 傳回 `3` (第一個群組的起始位置)。 |
| `IndexRegExGroup('abc123','([a-z])([0-9]+)',2)` | 傳回 `4`，這是第二個群組的起始位置。在輸入 Regex 模式中，字串 `([0-9]+)` 對應於第二個群組。 |
| `IndexRegExGroup('ABC123','([a-z])([0-9]+)',1)` | 傳回 `0`，因為 IndexRegExGroup() 區分大小寫。 |
| `IndexRegExGroupI('ABC123','([a-z])([0-9]+)',1)` | 傳回 `3`。使用不區分大小寫的函數變體 IndexRegExI()。 |

## 使用情境

此函數的使用案例包括：

* 辨識特定文字模式在較大文字內文中出現的位置。例如，您可能想要知道電子郵件地址的網域在一系列長電子郵件中的使用位置。

* IndexRegExGroup() 特別適合用於進階資料處理，並且通常在更長、更複雜的轉換的第一步中使用。使用其他規則運算式函數 (例如 ExtractRegExGroup()、MatchRegEx() 和 CountRegEx()) 通常更容易解決問題，但有時 IndexRegExGroup() 可以提供這些函數無法提供的解決方案。

## 範例 1 – 用於尋找 ISBN 元件位置的載入指令碼

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

#### 載入指令碼和結果

概述

開啟資料載入編輯器並將下面的載入指令碼新增至新的區段。

載入指令碼包含：

* 建立變數 `ISBN_RegEx` 來儲存我們想要使用的規則運算式。
* 書店想要從多個供應商處訂購之書籍的一組 ISBN 代碼。

對於每個 ISBN 代碼，我們需要擷取下列各項的起始位置：

* EAN

* 群組

* 註冊者

* 出版品

* 總和檢查碼

載入指令碼

```
SET ISBN_RegEx = 'ISBN[ ]*([0-9]{3})-([0-9]{1})-([0-9]{4})-([0-9]{4})-([0-9]{1})';

ISBN:
LOAD Supplier,
IndexRegExGroup(Books,'$(ISBN_RegEx)',1) AS EAN,
IndexRegExGroup(Books,'$(ISBN_RegEx)',2) AS Group,
IndexRegExGroup(Books,'$(ISBN_RegEx)',3) AS Registrant,
IndexRegExGroup(Books,'$(ISBN_RegEx)',4) AS Publication,
IndexRegExGroup(Books,'$(ISBN_RegEx)',5) AS Checksum;
// Split the ISBN with the Group function in a preceding load to avoid generating a cartesian product
LOAD *,
ExtractRegEx(SupplierBooks, '$(ISBN_RegEx)') AS Books
INLINE [
Supplier, SupplierBooks
Supplier 1, ISBN 123-3-1234-1234-0 ISBN 012-2-0123-0123-4 ISBN 000-1-0123-0123-2 ISBN 234-5-2345-2345-1 ISBN 555-2-5555-5555-3 ISBN 222-4-2222-2222-2
Supplier 2, ISBN 000-0-3333-3333-3 ISBN 333-3-3333-3333-3 ISBN 555-1-5151-5151-3 ISBN 232-1-2323-2323-1 ISBN 008-0-7777-7777-3 ISBN 888-0-9999-0000-0
];
```

結果

載入資料並開啟工作表。建立新的表格並將這些欄位新增為維度：

* `Supplier`

* `EAN`

* `Group`

* `Registrant`

* `Publication`

* `Checksum`

結果表格

| Supplier | EAN | Group | Registrant | Publication | Checksum |
| --- | --- | --- | --- | --- | --- |
| Supplier 1 | 6 | 10 | 12 | 17 | 22 |
| Supplier 1 | 6 | 10 | 12 | 17 | 22 |
| Supplier 1 | 6 | 10 | 12 | 17 | 22 |
| Supplier 1 | 6 | 10 | 12 | 17 | 22 |
| Supplier 1 | 6 | 10 | 12 | 17 | 22 |
| Supplier 1 | 6 | 10 | 12 | 17 | 22 |
| Supplier 2 | 6 | 10 | 12 | 17 | 22 |
| Supplier 2 | 6 | 10 | 12 | 17 | 22 |
| Supplier 2 | 6 | 10 | 12 | 17 | 22 |
| Supplier 2 | 6 | 10 | 12 | 17 | 22 |
| Supplier 2 | 6 | 10 | 12 | 17 | 22 |
| Supplier 2 | 6 | 10 | 12 | 17 | 22 |

在本案例中，每本書都包含位於相同位置的元件。

## 範例 2 – 用於尋找 URL 位置的圖表運算式 (使用 IndexRegEx() 比較)

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

#### 圖表運算式和結果

概述

開啟資料載入編輯器並將下面的載入指令碼新增至新的區段。

載入指令碼包含：

* 名為 `Correspondence` 的表格，其中名為 `EmailBody` 的欄位中包含電子郵件文字。

* 包含網址的電子郵件內容。

我們想要找到第二個 URL (如果存在) 的位置以及以下元件的位置：

* 網域

* 路徑

我們希望使用單一規則運算式來執行所有操作。

載入指令碼

```
Correspondence:

Load * Inline `
ID	EmailBody
1	Thanks again for this morning's meeting! You can find the meeting minutes posted here: https://example.com/resourceexample. If you still have any questions, always feel free to ask me or one of the other team members. Here are a few learning resources that might help you: http://www.example.ca/training1.pptx http://www.example.ca/training2.pptx http://www.example.ca/training3.pptx Thanks again!
2	Hi, you'll want to visit our company website for that, it's available at https://www.example.se.
3	Hello all, I just wanted to let you know that our online stores are now up and running! I couldn't be more excited. We are already seeing quite a bit of traffic and volume sold, which is very promising! For Product A, go to https://www.examplestore1.com/products. For Product B, you'll want go to https://www.examplestore2.com/products. Product C, go check out https://www.examplestore3.com/products. Cheers!

` (delimiter is '\t');
```

結果

載入資料並開啟工作表。建立新的表格並將此欄位新增為維度：

* `ID`

新增這些計算維度：

* URL 2 位置：

  ```
  =IndexRegExGroup(EmailBody,'([a-zA-Z0-9]+):\/\/(([a-zA-Z0-9]+\.)*([a-zA-Z0-9]+)\.([a-zA-Z0-9]+))((\/[a-zA-Z0-9]+)*(\/([a-zA-Z0-9]+)(\.[a-zA-Z0-9]+)?))?',2)
  ```

* URL 2 網域位置：

  ```
  =IndexRegExGroup(EmailBody,'([a-zA-Z0-9]+):\/\/(([a-zA-Z0-9]+\.)*([a-zA-Z0-9]+)\.([a-zA-Z0-9]+))((\/[a-zA-Z0-9]+)*(\/([a-zA-Z0-9]+)(\.[a-zA-Z0-9]+)?))?',2,2)
  ```

* URL 2 路徑位置：

  ```
  =IndexRegExGroup(EmailBody,'([a-zA-Z0-9]+):\/\/(([a-zA-Z0-9]+\.)*([a-zA-Z0-9]+)\.([a-zA-Z0-9]+))((\/[a-zA-Z0-9]+)*(\/([a-zA-Z0-9]+)(\.[a-zA-Z0-9]+)?))?',6,2)
  ```

結果表格

| ID | URL 2 位置 | URL 2 網域位置 | URL 2 路徑位置 |
| --- | --- | --- | --- |
| 1 | 274 | 281 | 295 |
| 2 | 0 | 0 | 0 |
| 3 | 298 | 306 | 327 |

IndexRegEx() 傳回整個 URL 的起始位置，而 IndexRegExGroup() 傳回 URL 各個部分的起始位置，對應到我們使用的 group 值。 `ID` 值為 2 的記錄只包含一個 URL，因此會傳回值 0。

所有圖表運算式都使用相同的規則運算式來傳回不同的資訊。規則運算式中定義的群組之細分如下。

規則運算式中的群組

| 群組編號 | 規則運算式 | URL 部分 |
| --- | --- | --- |
| 1 | ([a-zA-Z0-9]+) | 通訊協定 |
| 2 | (([a-zA-Z0-9]+\.)\*([a-zA-Z0-9]+)\.([a-zA-Z0-9]+)) | 網域 (第 3、4 和 5 組) |
| 3 | ([a-zA-Z0-9]+\.)\* | 根網域 |
| 4 | ([a-zA-Z0-9]+) | 頂層網域 |
| 5 | ([a-zA-Z0-9]+) | 路徑 (第 7、8、9 和 10 組) (選用) |
| 6 | ((\/[a-zA-Z0-9]+)\*(\/([a-zA-Z0-9]+)(\.[a-zA-Z0-9]+)?))? | 子目錄 |
| 7 | (\/[a-zA-Z0-9]+)\* | 檔案（第 9 組和第 10 組） |
| 8 | (\/([a-zA-Z0-9]+)(\.[a-zA-Z0-9]+)?) | 檔案 |
| 9 | ([a-zA-Z0-9]+) | 檔案名稱 |
| 10 | (\.[a-zA-Z0-9]+)? | 副檔名 (選用) |

## 瞭解更多資訊

* [在載入指令碼和圖表運算式中使用規則運算式](https://help.qlik.com/zh-TW/sense/May2025/Subsystems/Hub/Content/Sense_Hub/Scripting/regular-expressions-qlik-analytics.htm)