# SubFieldRegEx - 指令碼與圖表函數

SubFieldRegEx() 會使用指定的規則運算式模式作為分隔符號，從輸入字串運算式中擷取文字。如果未找到相符項，則該函數將傳回空值。

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

語法：

SubFieldRegEx (text, regex\_delimiter [, field\_no])

傳回的資料類型： 字串

引數

| 引數 | 描述 |
| --- | --- |
| text | 要在其中搜尋規則運算式模式的輸入字串文字。 |
| regex\_delimiter | 包含要用作分隔符號之規則運算式的字串運算式。您可以在同一規則運算式模式中指定多個分隔符號，並用 `\|` (垂直線) 字元進行分隔。 |
| field\_no | 表示要擷取之細分編號的整數值。例如，指定值 `4` 以擷取第四個細分。此為選用引數。<br><br>無論該函數是在載入指令碼還是圖表運算式中使用，以下內容都適用：<br><br><ul> <li> <p>如果 <span>field_no</span> 為正，則函數將傳回一個值，從左到右識別相符項。</p> </li> <li> <p>如果 <span>field_no</span> 為負，則函數將傳回一個值，從右到左識別相符項。</p> </li> </ul><br><br>當在載入指令碼中使用該函數時：<br><br><ul> <li> <p>如果在 <span>LOAD</span> 陳述式中使用 <span>SubFieldRegEx()</span> 函數且省略 <span>field_no</span>，則函數會產生與相符項一樣多的記錄。</p> </li> <li> <p>如果使用 <span>SubFieldRegEx()</span> 載入數個欄位且所有項均未指定 <span>field_no</span> 引數，則會建立所有組合的 Cartesian 產品。</p> </li> </ul><br><br>當在圖表運算式中使用該函數時：<br><br><ul> <li> <p>如果省略 <span>field_no</span>，則預設值為 <code>1</code>。</p> </li> </ul> |

函數範例

| 範例 | 結果 |
| --- | --- |
| `SubFieldRegEx('a,b c;1 2,3',' \|,\|;', 1)` | 傳回 `a`。在此範例中，指定了三個分隔符號 (空格、逗號和分號)。 |
| `SubFieldRegEx('a;b;c;1;2;3',';', 2)` | 傳回 `b`。在本案例中，僅指定了一個分隔符號 (分號)。 |
| `SubFieldRegEx('a,b c;1 2,3',' \|,\|;', 4)` | 傳回 `1`。 |

## 什麼情況下使用

您可以使用 SubFieldRegEx() 從分隔清單中擷取多個單獨的值。例如，如果您有一個逗號分隔清單包含在較大自然語言區塊中，則可以使用此函數將清單中的每個單獨值隔離到資料模型中的新記錄中。

## 範例 1 – 用於依交易分隔標記的載入指令碼

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

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

概述

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

載入指令碼包含：

* 表格 `Transaction_By_Tag`，最初包含交易資料。處理該資料以將個別標記值擷取到新記錄中。

* 建立一個欄位 `TransactionTag` 來儲存擷取的標記值。

在包含交易標記的欄位中，文字字串中包含多個標記，並以分隔符號分隔。分隔符號可以是下列任意字元： , | ;

載入指令碼

```
Transaction_By_Tag:

Load SubFieldRegEx(Tags, ',|;|\|') as TransactionTag,
* Inline [
ID	Product	Tags	Amount
1	Product A	Special order,Eligible for return,Warranty included	100.93
2	Product B	Ineligible for return|No warranty available	51.11
3	Product C	No warranty available	12.83
4	Product B	Special order;Liquidation sale;Warranty included	209.48
] (delimiter is '\t');

drop fields Tags;
```

結果

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

* `ID`

* `Product`

* `TransactionTag`

結果表格

| ID | Product | TransactionTag |
| --- | --- | --- |
| 1 | Product A | Eligible for return |
| 1 | Product A | Warranty included |
| 1 | Product A | Special order |
| 2 | Product B | No warranty available |
| 2 | Product B | Ineligible for return |
| 3 | Product C | No warranty available |
| 4 | Product B | Liquidation sale |
| 4 | Product B | Special order |
| 4 | Product B | Warranty included |

此範例示範了在載入指令碼中使用時，SubFieldRegEx() 為在輸入字串中找到的每個相符項傳回單獨的列。

## 範例 2 – 用於依交易分隔標記的圖表運算式

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

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

概述

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

載入指令碼包含：

* 表格 `Transaction_By_Tag`，包含交易資料。

* 欄位 `Tags`，包含文字字串，用於儲存與交易關聯的所有標記。

我們的要求：

* 在包含交易標記的欄位中，文字字串中包含多個標記，並以分隔符號分隔。分隔符號可以是下列任意字元： , | ;

* 我們希望使用圖表運算式將每個標記分隔到其自己的欄位中，最多可以將三個標記新增到交易中。

載入指令碼

```
Transaction_By_Tag:

Load * Inline [
ID	Product	Tags	Amount
1	Product A	Special order, Eligible for return, Warranty included	100.93
2	Product B	Ineligible for return | No warranty available	51.11
3	Product C	No warranty available	12.83
4	Product B	Special order;Liquidation sale;Warranty included	209.48
] (delimiter is '\t');
```

結果

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

* `ID`

* `Tags`

新增這些計算維度：

* 

  `=SubFieldRegEx(Tags, ',|;|\|', 1)`

* 

  `=SubFieldRegEx(Tags, ',|;|\|', 2)`

* 

  `=SubFieldRegEx(Tags, ',|;|\|', 3)`

結果表格

| ID | Tags | =SubFieldRegEx(Tags, ',\|;\|\\|', 1) | =SubFieldRegEx(Tags, ',\|;\|\\|', 2) | =SubFieldRegEx(Tags, ',\|;\|\\|', 3) |
| --- | --- | --- | --- | --- |
| 1 | Special order, Eligible for return, Warranty included | Special order | Eligible for return | Warranty included |
| 2 | Ineligible for return \| No warranty available | Ineligible for return | No warranty available | - |
| 3 | No warranty available | No warranty available | - | - |
| 4 | Special order;Liquidation sale;Warranty included | Special order | Liquidation sales | Warranty included |

## 瞭解更多資訊

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