# ReplaceRegExGroup - スクリプトおよびチャート関数

ReplaceRegExGroup() は、入力文字列と指定された複合正規表現パターン間の 1 つ以上の一致を置き換えた後の文字列を返します。一致するテキストを置き換えるテキストは、to\_str 引数で指定されます。関数は左から右に動作しますが、負の occurrence 値を指定すると、関数は右から左に読み取られます。

この関数は、大文字と小文字を区別する正規表現操作を実行します。代わりに、バリアント ReplaceRegExGroupI() を使用して、大文字と小文字を区別しない正規表現操作を実行することもできます。

構文:

ReplaceRegExGroup (text, regex, to\_str, group [, occurrence])

戻り値データ型： 文字列

引数

| 引数 | 説明 |
| --- | --- |
| text | to\_str からの新しいテキストで完全にまたは部分的に置き換える入力文字列テキスト。 |
| regex | テキストをいつ置き換えるかを定義する正規表現。この引数と text 引数の一致は置き換えられます。 |
| to\_str | text の既存のコンテンツを置き換えるために使用する新しいテキスト。 |
| group | 複合正規表現の場合のグループの数。<br><br>正規表現にグループが 1 つだけ含まれている場合は、代わりに ReplaceRegEx() 関数を使用します。または、`0` の group 値で ReplaceRegExGroup() を使用します。<br><br>負の group 値を指定すると、グループを右から左に検索できます。 |
| occurrence | 新しいテキストに置き換える一致数 (入力テキストと正規表現の間)。<br><br>これはオプションの引数です。省略した場合の既定は 0 です。値 0 が使用されるか、引数が省略された場合、text と regex 間のすべての一致は to\_str に置き換えられます。<br><br>occurrence に負の値を指定すると、一致を右から左に識別できます。 |

関数の例

| 例 | 結果 |
| --- | --- |
| `ReplaceRegExGroup('abc123','([a-z])([0-9]+)','x',0)` | `abx` を返します (完全一致の置換)。 |
| `ReplaceRegExGroup('abc123','([a-z])([0-9]+)','x',1)` | `abx123` を返します (一致する最初のグループのみを置換)。 |
| `ReplaceRegExGroup('abc123','([a-z])([0-9]+)','x',2)` | `abcx` を返します (一致する 2 番目のグループのみを置換)。 |
| `ReplaceRegExGroup('abc123abc123','([a-z])([0-9]+)','x',1,2)` | `abc123abx123` を返します。入力テキストの最初のグループと正規表現パターンの間には 2 つの一致があります。ただし、置き換えられるのは 2 番目のみです。 |
| `ReplaceRegExGroup('ABC123ABC123','([a-z])([0-9]+)','x',1)` | `ABC123ABC123` を返します。つまり、テキストは置き換えられません。これは、関数が大文字と小文字を区別し、関数の大文字と小文字を区別しないバージョンが使用されないためです。 |
| `ReplaceRegExGroupI('ABC123ABC123','([a-z])([0-9]+)','x',1)` | `ABx123ABx123` を返します。この例では、関数の大文字と小文字を区別しないバリアント ReplaceRegExGroupI() が使用されているため、テキストの置換が行われます。 |

## 使用に適しているケース

特にデータ内の複雑なパターンを識別する必要がある場合、ReplaceRegExGroup() を使用してテキストを変更し、書式設定とコンプライアンスの標準を満たすことができます。この関数の追加の group 引数を使用すると、大きなテキスト パターンの特定の部分のみを更新できます。たとえば、URL の特定の部分を更新できます。

必要に応じて、この関数を使用して、個人を特定できる情報 (PII) などの機密情報をマスクし、アプリを分析するユーザーに表示されないようにすることもできます。

## 例 1 - URL のドメインを更新するロード スクリプト

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

#### ロード スクリプトと結果

概要

データ ロード エディターを開き、以下のロード スクリプトを新しいセクションに追加します。

ロード スクリプトには次が含まれています:

* 特定の URL を識別して更新する入力文字列のリストが最初に含まれるテーブル `URLs`。これらの URL のドメインを更新します。

* 処理された URL を含む変更された入力テキストを含む項目、`ResourceURL_Updated` の作成。

* 処理する URL を識別するために使用される正規表現を格納する変数 `URL_RegEx`。

要件:

* 各入力文字列には 2 つの URL が含まれており、最初の URL にはリソース URL が指定されています。リストされている最初の URL 以外の URL は更新しません。

* 更新する URL は、&lt;https または https&gt;://&lt;ドメイン&gt;.com/&lt;リソース パス&gt; の形式である必要があります。リソース パスはオプションですが、この例では使用されません。

ロード スクリプト

```
Set URL_RegEx = '(https?):\/\/(([a-zA-Z0-9]+)\.([a-zA-Z0-9]*)\.??([a-zA-Z0-9]*))\/?((([a-zA-Z0-9]*)*)((\/?([a-zA-Z0-9]*)*))*)\/{0,1}([\.,;]+(?=\s))*?';

URLs:

Load
ReplaceRegExGroup(URLsList, '$(URL_RegEx)', 'replacement-server', 2,1) as ResourceURL_Updated;

Load * Inline `
URLsList
The resource is located at https://testserver.com/files. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/files/worksheet. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/files/book. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/files/form. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/datamodel. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/resourcenew. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/page. For more help, see: https://support.company.com.
The resource is located at https://testserver.com/page/overview. For more help, see: https://support.company.com.
` (delimiter is '\t');
```

結果

データをロードしてシートを開きます。新しいテーブルを作成し、この項目を軸として追加します:

* `ResourceURL_Updated`

結果テーブル

| ResourceURL\_Updated |
| --- |
| The resource is located at https://replacement-server/datamodel. For more help, see: https://support.company.com. |
| The resource is located at https://replacement-server/files. For more help, see: https://support.company.com. |
| The resource is located at https://replacement-server/files/book. For more help, see: https://support.company.com. |
| The resource is located at https://replacement-server/files/form. For more help, see: https://support.company.com. |
| The resource is located at https://replacement-server/files/worksheet. For more help, see: https://support.company.com. |
| The resource is located at https://replacement-server/page. For more help, see: https://support.company.com. |
| The resource is located at https://replacement-server/page/overview. For more help, see: https://support.company.com. |
| The resource is located at https://replacement-server/resourcenew. For more help, see: https://support.company.com. |

## 例 2 - URL のプロトコルを更新するチャートの数式

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

#### チャートの数式および結果

概要

データ ロード エディターを開き、以下のロード スクリプトを新しいセクションに追加します。

ロード スクリプトには次が含まれています:

* 特定の URL を識別して更新する入力文字列のリストを含むテーブル `URLsList`。サポート ウェブサイトへのリンクのプロトコルを http から https に更新します。

要件:

* 各入力文字列には 2 つの URL が含まれており、サポートウェブサイトのリンクは 2 番目の URL として表示されます。2 番目の URL 以外の URL は更新しません。

* 更新する URL は、&lt;https または https&gt;://&lt;ドメイン&gt;.com/&lt;リソース パス&gt; の形式である必要があります。リソース パスはオプションですが、この例では使用されません。

ロード スクリプト

```
URLs:

Load * Inline `
URLsList
The resource is located at https://testserver.com/files. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/files/worksheet. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/files/book. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/files/form. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/datamodel. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/resourcenew. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/page. For more help, see: http://support.company.com.
The resource is located at https://testserver.com/page/overview. For more help, see: http://support.company.com.
` (delimiter is '\t');
```

結果

データをロードしてシートを開きます。新しいテーブルを作成し、この項目を軸として追加します:

* `URLsList`

この計算軸を追加します。

```
=ReplaceRegExGroup(URLsList, '(https?):\/\/(([a-zA-Z0-9]+)\.([a-zA-Z0-9]*)\.??([a-zA-Z0-9]*))\/?((([a-zA-Z0-9]*)*)((\/?([a-zA-Z0-9]*)*))*)\/{0,1}([\.,;]+(?=\s))*?', 'https', 1,2)
```

結果テーブル

| URLsList | =ReplaceRegExGroup(URLsList, '(https?):\/\/(([a-zA-Z0-9]+)\.([a-zA-Z0-9]\*)\.??([a-zA-Z0-9]\*))\/?((([a-zA-Z0-9]\*)\*)((\/?([a-zA-Z0-9]\*)\*))\*)\/{0,1}([\.,;]+(?=\s))\*?', 'https', 1,2) |
| --- | --- |
| The resource is located at https://testserver.com/files. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/datamodel. For more help, see: https://support.company.com. |
| The resource is located at https://testserver.com/files/worksheet. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/files. For more help, see: https://support.company.com. |
| The resource is located at https://testserver.com/files/book. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/files/book. For more help, see: https://support.company.com. |
| The resource is located at https://testserver.com/files/form. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/files/form. For more help, see: https://support.company.com. |
| The resource is located at https://testserver.com/datamodel. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/files/worksheet. For more help, see: https://support.company.com. |
| The resource is located at https://testserver.com/resourcenew. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/page. For more help, see: https://support.company.com. |
| The resource is located at https://testserver.com/page. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/page/overview. For more help, see: https://support.company.com. |
| The resource is located at https://testserver.com/page/overview. For more help, see: http://support.company.com. | The resource is located at https://testserver.com/resourcenew. For more help, see: https://support.company.com. |

## 詳細を見る

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