# 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() のユース ケースには次のようなものがあります。

* 文字列内で特定のテキスト パターンが出現する回数をカウントします。たとえば、ドキュメントやメール メッセージ内のメール アドレス、電話番号、その他の情報のインスタンスを検索できます。

* データが特定の構文要件に一致するかどうかを検証します。 たとえば、データ入力時のエラーの発生を特定できます。

## 例 1 - 有効な年の範囲をカウントするロード スクリプト

[](https://help.qlik.com/en-US/sense/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/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) が削除されていますが、会社名と送信元のメール アドレスは保持されています。各メッセージの本文は、1 つの連続した文字列にまとめられています。つまり、すべての書式設定と改行文字は削除されています。

要件:

* メール アドレス 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 |

5 つのメッセージのうち、指定された正規表現パターンに一致するものが 3 つあります。各メッセージには 1 つの一致が含まれます。

情報メモMatchRegEx() 関数は、このようなタイプの検証ユース ケースにも役立ちます。

## 詳細を見る

* [ロード スクリプトとチャートの数式での正規表現の使用](https://help.qlik.com/ja-JP/sense/May2025/Subsystems/Hub/Content/Sense_Hub/Scripting/regular-expressions-qlik-analytics.htm)