# CountRegEx 脚本和图表功能

CountRegEx() 用于返回指定正则表达式模式在输入字符串文本中出现的次数。如果不匹配，则返回 0。

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

语法：

CountRegEx (text, regex)

返回数据类型： 数字

参数

| 参数 | 描述 |
| --- | --- |
| text | 要搜索正则表达式的输入字符串文本。 |
| regex | 要搜索的正则表达式模式。 |

函数示例

| 示例 | 结果 |
| --- | --- |
| `CountRegEx('abc123','[a-z]')` | 返回 `3`。 |
| `CountRegEx('abc123','[a-z][0-9]')` | 返回 `1`。 |
| `CountRegEx('abc123','[0-9]')` | 返回 `3`。 |
| `CountRegEx('ABC','[a-z]{3}')` | 返回 `0`。 |
| `CountRegExI('ABC','[a-z]{3}')` | 返回 `1`。由于使用的是 CountRegExI() 变体，文本搜索不区分大小写。 |

## 适合使用 CountRegEx 的情况

CountRegEx() 的用例包括：

* 计算特定文本模式在字符串中出现的次数。例如，您可以在文档和电子邮件信息中搜索电子邮件地址、电话号码和其他信息的实例。

* 验证数据是否符合特定语法要求。例如，您可以识别数据录入中出现的错误。

## 例 1 - 计算有效年份范围的加载脚本

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

#### 加载脚本和结果

概述

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

加载脚本包含：

* 一个名为 `Vehicles` 的表格，包含车辆清单，以及每辆车的状况和车型年份。

* 对于 `Vehicles` 中的每条记录，都要进行计算，以确定车辆的年份范围是否符合预期的语法。有效年份范围包括 1990 年代、2000 年代、2010 年代和 2020 年代。计算使用 CountRegEx() 函数。

加载脚本

```
Vehicles:
Load CountRegEx(Model_YearRange, '(^19[0-9][0-9]s$)|(^20[0-9][0-9]s$)') as Valid_Year_Data,
* Inline [
Vehicle_ID, Condition, Model_YearRange
1, Good,20100s
2,Fair,2020s
3, Poor,2020s
4, Good,210s
5, Excellent,2000s
6, Excellent,2020s
7, Excellent,202s
8, Good,2010s
9, Poor,2010ss
10, Poor,1990s
];
```

结果

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

* `Vehicle_ID`

* `Valid_Year_Data`

结果表

| Vehicle\_ID | Valid\_Year\_Data |
| --- | --- |
| 1 | 0 |
| 2 | 1 |
| 3 | 1 |
| 4 | 0 |
| 5 | 1 |
| 6 | 1 |
| 7 | 0 |
| 8 | 1 |
| 9 | 0 |
| 10 | 1 |

您还可以在工作表中添加 KPI，并添加这一衡量标准：

```
Sum(Valid_Year_Data)
```

该图表显示了具有有效 Model\_YearRange 值的记录总数。

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

## 例 2 - 加载脚本以统计文本字符串中的电子邮件地址数

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

#### 加载脚本和结果

概述

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

加载脚本包含：

* 一个名为 `Employees` 的数据表，其中包含公司员工的数据。每个员工的所有电子邮件地址都包含在一个字符串中。

* 创建一个字段，`NoOfEmails`，用于计算每个员工的输入字符串中包含多少个电子邮件地址。

加载脚本

```
Employees:

Load CountRegExI(Emails, '@[a-zA-Z0-9.-]*\.?[a-zA-Z0-9]*\.com') as NoOfEmails,
* Inline `
ID	Emails
1	john_doe@test.com john_michael_doe@example.com j.doe123@example123.com
2	jane_doe@test.com
3	Michael_Andrews@test.com Michael_John_Andrews@example.com
4	charlie_kent@support.test.com charlie_peter_kent@support.example.com cp.kent.1985@test123.com c_p_kent@test123.com
5	frances_johnson@test.com frances_johnson@example.com
6	rebecca_smith@hr.test.com
` (delimiter is '\t');
```

结果

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

* `ID`

* `Emails`

* `NoOfEmails`

结果表

| ID | Emails | NoOfEmails |
| --- | --- | --- |
| 1 | john\_doe@test.com john\_michael\_doe@example.com j.doe123@example123.com | 3 |
| 2 | jane\_doe@test.com | 1 |
| 3 | Michael\_Andrews@test.com Michael\_John\_Andrews@example.com | 2 |
| 4 | charlie\_kent@support.test.com charlie\_peter\_kent@support.example.com cp.kent.1985@test123.com c\_p\_kent@test123.com | 4 |
| 5 | frances\_johnson@test.com frances\_johnson@example.com | 2 |
| 6 | rebecca\_smith@hr.test.com | 1 |

## 例 3 - 统计字符串出现次数的图表表达式

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

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

概述

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

加载脚本中包含了一些企业信件的文本，这些信件是发送给某企业员工的。除了公司名称和发送每封邮件的电子邮件地址外，数据中的任何个人身份信息 (PII) 都已清除。每封邮件的正文都被压缩成一个连续的字符串。换句话说，所有格式化字符和换行符都已删除。

我们的要求：

* 识别哪些邮件包含一个字符串，其中包括电子邮件地址 TestCompanyNameCorrespondence@test.com ，以及对这个地址的具体介绍，表明它是发件人。

* 我们希望在确定哪些介绍方面具有灵活性，但同时也有一些明确的限制。导言 Sender 和 Sent by 都被认为是有效的。要实现这一目标，可以使用正则表达式模式。

* 电子邮件地址的大小写敏感性不在考虑之列。因此，搜索应区分大小写。

加载脚本

```
Correspondences:
Load * Inline [
ID|MessageBody
1|Sent by-- TestCompanyNameCorrespondence@test.com Good morning TestCompanyName team! I hope you are all well. I am just reaching out about the fabulous benefits package we launched just year. Our goal is to take a poll of what you all think. Can you please rate the new options from 1-10 in a response to this email, by this Tuesday? Thank you! 
2|Sender: TestCompanyNameIT@test.com Did you know you can access our IT portal anytime, from any of your company devices? It's true! Reach out to your team lead to learn more.
3|Sender: TESTCOMPANYNAMECORRESPONDENCE@test.COM URGENT: This is a message to inform employees of an ongoing issue with our local branch. The branch will be closed until further notice. Thank you
4|Sender: TestCompanyNameVolunteering@test.com Dear team! We are looking for volunteers to help with this year's charity event! We need 40 volunteers to help with the event. If you are interested, please send an email to your direct supervisor. Thanks, TestCompanyName team members, for helping make the world a better place!
5|Sender is TestCompanyNameCorrespondence@test.com Hi folks, it's time to announce the annual TestCompanyName staff party! The party will be taking place at the office this year. Please make sure to RSVP, and we hope to see you there! Sincerely, the TestCompanyName management team
] (delimiter is |);
```

结果

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

* ID

* MessageBody

添加以下度量：

```
=CountRegExI(MessageBody,'(Sender.*TestCompanyNameCorrespondence@test.com)|(Sent by.*TestCompanyNameCorrespondence@test.com)')
```

在测量属性中，将总计函数设置为 Sum。这将配置表格顶部的总计行，以计算参与计数的记录总数。

该指标计算信息内容中与指定正则表达式模式匹配的次数。在正则表达式模式中，.\* 表示除换行符之外的任意字符的任意个数。该模式还考虑了电子邮件地址引入方式的变化：Sender 和 Sent by 都是有效匹配。该函数的 CountRegExI() 变体可确保搜索不区分大小写。

结果表

| ID | MessageBody | =CountRegExI(MessageBody,'(Sender.\*TestCompanyNameCorrespondence@test.com)\|(Sent by.\*TestCompanyNameCorrespondence@test.com)') |
| --- | --- | --- |
| Totals | - | 3 |
| 1 | Sent by-- TestCompanyNameCorrespondence@test.com Good morning TestCompanyName team! I hope you are all well. I am just reaching out about the fabulous benefits package we launched just year. Our goal is to take a poll of what you all think. Can you please rate the new options from 1-10 in a response to this email, by this Tuesday? Thank you! | 1 |
| 2 | Sender: TestCompanyNameIT@test.com Did you know you can access our IT portal anytime, from any of your company devices? It's true! Reach out to your team lead to learn more. | 0 |
| 3 | Sender: TESTCOMPANYNAMECORRESPONDENCE@TEST.COM URGENT: This is a message to inform employees of an ongoing issue with our local branch. The branch will be closed until further notice. Thank you | 1 |
| 4 | Sender: TestCompanyNameVolunteering@test.com Dear team! We are looking for volunteers to help with this year's charity event! We need 40 volunteers to help with the event. If you are interested, please send an email to your direct supervisor. Thanks, TestCompanyName team members, for helping make the world a better place! | 0 |
| 5 | Sender is TestCompanyNameCorrespondence@test.com Hi folks, it's time to announce the annual TestCompanyName staff party! The party will be taking place at the office this year. Please make sure to RSVP, and we hope to see you there! Sincerely, the TestCompanyName management team | 1 |

在这五条信息中，有三条符合指定的正则表达式模式。每条信息包含一个匹配容器。

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

## 了解详情

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