# CountRegEx - 指令碼與圖表函數

CountRegEx() 會傳回輸入字串文字中指定規則運算式的出現次數。如果沒有任何相符項，則會傳回 0。

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

語法：

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() 的用例範例包括：

* 計算特定文字模式在字串中出現的次數。例如，您可以搜尋文件和電子郵件訊息中電子郵件地址、電話號碼和其他資訊出現的情況。

* 驗證資料是否符合特定語法要求。 例如，您可以識別資料輸入中出現的錯誤。

## 範例 1 – 用於計算有效年份範圍的載入指令碼

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

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

概述

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

載入指令碼包含：

* 名為 `Vehicles` 的表格包含車輛清單，以及每輛車的狀況和型號年份。

* 對於 `Vehicles` 中的每筆記錄，進行計算以確定車輛的年份範圍是否符合預期語法。有效年份範圍包括 20 世紀 90 年代、21 世紀 00 年代、21 世紀 10 年代和 21 世紀 20 年代。此計算是使用 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/sense/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/sense/Subsystems/Hub/Content/Sense_Hub/Scripting/StringFunctions/CountRegEx.htm)

#### 圖表運算式和結果

概述

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

載入指令碼包含傳送給組織員工之大量公司信件中的文字。除了公司名稱和傳送每封信件的電子郵件地址之外，資料中的所有個人識別資訊 (PII) 均已清除。每封郵件的內文都被摺疊成一個連續的字串。換句話說，已移除所有格式和換行符。

我們的要求：

* 識別哪些郵件所含字串中包含電子郵件地址 TestCompanyNameCorrespondence@test.com，以及表明該地址是寄件者的具體介紹。

* 我們希望能夠靈活地識別介紹的內容，但也仍有一些明確的限制。介紹 Sender 和 Sent by 均視為有效。為了實現這個目標，可以使用 Regex 模式。

* 電子郵件地址的大小寫敏感度不是考慮因素。因此，搜尋應該不區分大小寫。

載入指令碼

```
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)')
```

在量值屬性中，將 Totals 函數設定為 Sum。這會將表格頂部的總計列設定為計算參與計數的記錄總數。

此量值會計算郵件內容中指定 Regex 模式的相符項數目。在 Regex 模式中，.\* 表示除換行符之外的任意數量的任意字元。此模式也考慮了電子郵件地址引入方式的變體：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 |

在這五封郵件中，有三封郵件與指定的 Regex 模式相符。五封郵件包含一個相符項。

資訊備註MatchRegEx() 函數也很適合這些類型的驗證使用案例。

## 瞭解更多資訊

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