# IndexRegEx - 脚本和图表功能

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

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

语法：

IndexRegEx (text, regex [, count])

返回数据类型： 整数

参数

| 参数 | 描述 |
| --- | --- |
| text | 要搜索正则表达式的输入字符串文本。 |
| regex | 用于搜索输入字符串的正则表达式。 |
| count | 匹配的编号。当正则表达式在文本中可能有多个匹配项时，这个功能非常有用。例如，指定 `4` 提取第四个匹配项的位置。<br><br>这是可选参数。如果未指定，默认为 1。您可以指定一个负值，从右向左搜索匹配项。 |

函数示例

| 示例 | 结果 |
| --- | --- |
| `IndexRegEx('abc123','[a-z][0-9]+')` | 返回 `3`（第一个匹配项的起始位置）。 |
| `IndexRegEx('abc123','[a-z][0-9]+',2)` | 返回 `0` （正则表达式没有第二个匹配项）。 |
| `IndexRegEx('ABC123','[a-z][0-9]+')` | 返回 `0` ，因为 IndexRegEx() 区分大小写。 |
| `IndexRegExI('ABC123','[a-z][0-9]+')` | 返回 `3`。使用不区分大小写的函数变体，IndexRegExI()。 |

## 适合使用它的情况

该函数的用例包括：

* 识别大段文字中出现特定文字模式的位置。例如，您可能想知道在一系列长电子邮件中使用电子邮件地址模式的位置。

* IndexRegEx() 对高级数据处理特别有用，常用于较长、较复杂转换的第一步。使用其他正则表达式函数（如 ExtractRegEx() 、MatchRegEx() 和 CountRegEx()）通常更容易解决问题，但有时 IndexRegEx() 可能会提供这些函数无法提供的解决方案。

## 例 1 - 加载脚本以按 ISBN 识别图书

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

#### 加载脚本和结果

概述

打开数据加载编辑器，并将下面的加载脚本添加到新部分。

加载脚本包含：

* `ISBN` 表格中名为 `EmailBody` 的字段包含电子邮件内容。

我们希望从电子邮件的自然语言中找到所有 13 位 ISBN 编码的位置。

加载脚本

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

ISBN:
LOAD IndexRegEx(EmailText,'$(ISBN_RegEx)',1) AS ISBNCode1,
IndexRegEx(EmailText,'$(ISBN_RegEx)',2) AS ISBNCode2,
IndexRegEx(EmailText,'$(ISBN_RegEx)',3) AS ISBNCode3,
* INLINE `
EmailText
Hi there! I have some books that I'm interested in ordering from your distribution center. Just wanted to make sure they were in stock before I put the order in on the system - do you think you could tell me whether you have these in stock, and how many you might be able to ship us? Thanks! Item 1: ISBN 123-3-1234-1234-0. This is one I've had a really hard time getting a hold of lately. Do you think you would have 5 in stock? If not, can I put a hold on the next 5 you get? Item 2: ISBN 012-2-0123-0123-4 ... This one is not high-priority, but if you have one, I'll take it! Item 3: ISBN 000-1-0123-0123-2. Customers have been requesting this one for a long time, and we haven't had it in stock for years due to it being out of print. Any chance you might have 7 of them? Thanks!
` (delimiter is '/t/';
```

结果

加载数据并打开工作表。创建新表并将这些字段添加为维度：

* `ISBNCode1`

* `ISBNCode2`

* `ISBNCode3`

结果表

| ISBNCode1 | ISBNCode2 | ISBNCode3 |
| --- | --- | --- |
| 301 | 487 | 588 |

## 例 2 - 用图表表达式识别有辅助电话号码的联系人

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

#### 图表表达式和结果

概述

打开数据加载编辑器，并将下面的加载脚本添加到新部分。

加载脚本包含：

* 一个名为 `BusinessContactInfo` 的表格，其中包含在各公司网站上找到的公司信息。

* `ContactInfo` 是一个包含公司电话号码的字段。我们想确定哪些公司有多个电话号码。

我们的要求：

* 电话号码必须是 10 位数的 NANP 电话号码。

* 我们希望允许用括号将地区代码括起来。

* 我们希望在每个电话号码段之间留出一个空格或连字符。

加载脚本

```
BusinessContactInfo:

Load * Inline `
ID	CompanyName	ContactInfo
1	Company A	(123) 456-7890 (023) 123-4567
2	Company B	0123456790 1357913579 0246802468
3	Company C	234-567-8901

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

结果

加载数据并打开工作表。创建新表并将该字段添加为维度：

* `CompanyName`

将此计算维度添加到表格中：

```
=if(IndexRegEx(ContactInfo, '\({0,1}[0-9]{3}\){0,1}[ -]{0,1}[0-9]{3}[ -]{0,1}[0-9]{4}', 2)=0, 'No secondary phone number(s)', 'Has secondary phone number(s)')
```

结果表

| CompanyName | =if(IndexRegEx(ContactInfo, '\({0,1}[0-9]{3}\){0,1}[ -]{0,1}[0-9]{3}[ -]{0,1}[0-9]{4}', 2)=0, 'No secondary phone number(s)', 'Has secondary phone number(s)') |
| --- | --- |
| Company A | Has secondary phone number(s) |
| Company B | Has secondary phone number(s) |
| Company C | No secondary phone number(s) |

`Company A` 有一个辅助电话号码。`Company B` 有两个辅助电话号码。`Company C` 有三个辅助电话号码。

信息注释MatchRegEx() 函数也适用于这类验证用例。

## 了解详情

* [在加载脚本和图表表达式中使用正则表达式](https://help.qlik.com/zh-CN/sense/November2025/Subsystems/Hub/Content/Sense_Hub/Scripting/regular-expressions-qlik-analytics.htm)