# 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(개인 식별 정보)의 데이터가 이미 삭제되었습니다. 각 메시지의 본문은 연속된 단일 문자열로 축소되었습니다. 즉, 모든 서식 및 줄 바꿈 문자가 제거되었습니다.

요구 사항:

* 발신자임을 나타내는 이메일 주소 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개입니다. 각 메시지에는 일치하는 항목이 하나씩 포함되어 있습니다.

정보 메모MatchRegEx() 함수는 이러한 유형의 유효성 검사 사용 사례에도 유용합니다.

## 자세한 정보

* [로드 스크립트 및 차트 표현식에서 정규 표현식 사용](https://help.qlik.com/ko-KR/sense/May2026/Subsystems/Hub/Content/Sense_Hub/Scripting/regular-expressions-qlik-analytics.htm)