# SubFieldRegEx 脚本和图表功能

SubFieldRegEx() 使用指定的正则表达式模式作为分隔符，从输入字符串表达式中提取文本。如果没有找到匹配项，函数将返回空值。

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

语法：

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 的情况

您可以使用 SubFieldRegEx() 从分隔列表中提取多个单独的值。例如，如果您有一个逗号分隔的列表包含在一个更大的自然语言块中，您可以使用该函数将列表中的每个单独值分离到数据模型中的新记录中。

## 例 1 - 加载脚本，按交易分隔标签

[](https://help.qlik.com/en-US/cloud-services/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/cloud-services/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-CN/cloud-services/Subsystems/Hub/Content/Sense_Hub/Scripting/regular-expressions-qlik-analytics.htm)