# IndexRegExGroup - 脚本和图表功能

IndexRegEx() 搜索输入字符串，并返回指定的复合正则表达式模式第 n 次出现的起始位置。可选的第四个参数 count 用于提供 n 的值，如果省略，则值为 1。字符串中的位置从左至右从 1 开始编号。如果没有找到匹配项，函数返回 0。

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

语法：

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

返回数据类型： 整数

参数

| 参数 | 描述 |
| --- | --- |
| text | 要搜索正则表达式的输入字符串文本。 |
| regex | 用于搜索输入字符串的正则表达式。 |
| group | 在复合正则表达式的情况下，组的编号。<br><br>`0` 值为 group 则返回整个正则表达式的索引。但是，如果正则表达式只需要返回整个匹配的索引，则可以使用 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` ，即第二个群组的起始位置。在输入的正则表达式模式中，字符串 `([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 的电子邮件内容。

我们希望找到第二个 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-CN/sense/November2025/Subsystems/Hub/Content/Sense_Hub/Scripting/regular-expressions-qlik-analytics.htm)