---
schema_version: "1"
id: 956a70c5b2d77092addde59c26403624803b251dc0d0ed5050187fa950305d9b
title: replace
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/dataweave/2.7/dw-core-functions-replace.md
canonical_url: https://docs.mulesoft.com/jp/dataweave/2.7/dw-core-functions-replace
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 8253ccab29ef3393622c2e85c523b352c3a52f12154f1f360518a19bc2b3f252
description: MuleSoft Documentation Site
release: "2.7"
latest: false
---
# replace

## [](#replace1)replace(text: String, matcher: Regex): ((Array<String>, Number) -> String) -> String

文字列置換を実行します。

このバージョンの ​`replace`​ は、文字列の一部を照合する Java 正規表現を受け入れます。入力文字列の一致部分の置換文字列を指定するには、​`with`​ ヘルパー関数を使用する必要があります。

### [](#パラメーター)パラメーター

<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 25%;">
<col style="width: 75%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">名前</th>
<th class="tableblock halign-left valign-top">説明</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>text</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">照合する文字列。</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>matcher</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">入力 ​<code>text</code>​ 文字列内の文字を照合する Java 正規表現。</p></td>
</tr>
</tbody>
</table>

### [](#例)例

取得元の最初の例では、2 番目のハイフン (このハイフンを含む) までのすべての文字 (​`123-456-`​) を空の値で置き換えるため、最後の 4 桁の数字が返されます。2 番目の例では、入力文字列内の文字 ​`b13e`​ をハイフン (​`-`​) で置き換えます。

#### [](#ソース)ソース

```DataWeave
%dw 2.0
output application/json
---
["123-456-7890" replace /.*-/ with(""), "abc123def" replace /[b13e]/ with("-")]
```

#### [](#出力)出力

```JSON
[ 7890, "a-c-2-d-f" ]
```

### [](#例-2)例

次の例では、入力文字列内の数値 ​`123`​ を ​`ID`​ で置き換えます。この例では正規表現 ​`(\d+)`​ が使用されています。​`\d`​ メタ文字は 0 ～ 9 の任意の数字を意味し、​`` `​ は、1 桁以上の数字が出現可能であることを意味します。 ` ``​ がない場合、1 桁の数字ごとに 1 つの ​`ID`​ が出力に含まれます。この例では、中置表記を使用して式を記述する方法と、前置表記を使用して式を記述する方法も示しています。

#### [](#ソース-2)ソース

```DataWeave
%dw 2.0
output application/json
---
[ "my123" replace /(\d+)/ with("ID"), replace("myOther123", /(\d+)/) with("ID") ]
```

#### [](#出力-2)出力

```JSON
[ "myID", "myOtherID" ]
```

## [](#replace2)replace(text: String, matcher: String): ((Array<String>, Number) -> String) -> String

文字列置換を実行します。

このバージョンの ​`replace`​ は、指定された文字列の一部を照合する文字列を受け入れます。入力文字列の一致部分の置換文字列を渡すには、​`with`​ ヘルパー関数を使用する必要があります。

### [](#パラメーター-2)パラメーター

<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 25%;">
<col style="width: 75%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">名前</th>
<th class="tableblock halign-left valign-top">説明</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>text</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">照合する文字列。</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>matcher</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">入力 ​<code>text</code>​ 文字列内の文字を照合するための文字列。</p></td>
</tr>
</tbody>
</table>

### [](#例-3)例

次の例では、入力文字列からの数値 ​`123`​ を、​`with`​ 関数で渡された文字 ​`ID`​ で置き換えます。

#### [](#ソース-3)ソース

```DataWeave
%dw 2.0
output application/json
---
{ "replace": "admin123" replace "123" with("ID") }
```

#### [](#出力-3)出力

```JSON
{ "replace": "adminID" }
```

## [](#replace3)replace(text: Null, matcher: Any): ((Nothing, Nothing) -> Any) -> Null

`replace`​ を ​`null`​ 値に使用できるようにするヘルパー関数。

_DataWeave バージョン 2.4.0 で導入されました。_