%dw 2.0
output application/json
---
["123-456-7890" replace /.*-/ with(""), "abc123def" replace /[b13e]/ with("-")]
DataWeave
replace
replace(text: String, matcher: Regex): ((Array<String>, Number) -> String) -> String
Performs string replacement.
This version of replace
accepts a Java regular expression for matching
part of a string. It requires the use of the with
helper function to
specify a replacement string for the matching part of the input string.
Parameters
Name | Description |
---|---|
|
A string to match. |
|
A Java regular expression for matching characters in the input |
Example
The first example in the source replaces all characters up to and including
the second hyphen (123-456-
) with an empty value, so it returns the last
four digits. The second replaces the characters b13e
in the input string
with a hyphen (-
).
Source
Output
[ 7890, "a-c-2-d-f" ]
JSON
Example
This example replaces the numbers 123
in the input strings with ID
. It
uses the regular expression (\d+)
, where the \d
metacharacter means any
digit from 0-9, and +
means that the digit can occur one or more times.
Without the +
, the output would contain one ID
per digit. The example
also shows how to write the expression using infix notation, then using
prefix notation.
Source
%dw 2.0
output application/json
---
[ "my123" replace /(\d+)/ with("ID"), replace("myOther123", /(\d+)/) with("ID") ]
DataWeave
Output
[ "myID", "myOtherID" ]
JSON
replace(text: String, matcher: String): ((Array<String>, Number) -> String) -> String
Performs string replacement.
This version of replace
accepts a string that matches part of a specified
string. It requires the use of the with
helper function to pass in a
replacement string for the matching part of the input string.
Parameters
Name | Description |
---|---|
|
The string to match. |
|
The string for matching characters in the input |
Example
This example replaces the numbers 123
from the input string with
the characters ID
, which are passed through the with
function.
Source
%dw 2.0
output application/json
---
{ "replace": "admin123" replace "123" with("ID") }
DataWeave
Output
{ "replace": "adminID" }
JSON
replace(text: Null, matcher: Any): ((Nothing, Nothing) -> Any) -> Null
Helper function that enables replace
to work with a null
value.
Introduced in DataWeave version 2.4.0.