%dw 2.0
var myString = "mycompany.com"
output application/json
---
{
	"contains" : myString contains(/c.m/),
	"find" : myString find(/[m|n].|m$/),
	"match" : myString match(/([a-z]*).[a-z]*/),
	"matches" : myString matches(/([a-z]*).[a-z]*/),
	"replaceWith" : myString replace /\..*m/ with ".net",
	"scan" : myString scan(/([a-z]*).(com)/),
	"splitBy" : myString splitBy(/[.\/]/)
}
Use Regular Expressions in DataWeave
Several DataWeave functions accept regular expressions as arguments, which you can use to return or check for matches. You can also construct regular expressions that incorporate DataWeave expressions that include functions and variables.
Return Matches from a String
This example uses regular expressions in a number of DataWeave functions to return matches to an input variable "mycompany.com".
- 
containsreturnstruebased on a regular expression that matches part of the input string. - 
findreturns an array of indices that specify the matching locations in the input string. This function treats the input string as a string array. - 
matchreturns an array of substrings that match the regular expression. - 
matchesreturnstruebecause the regular expression matches the input string exactly. - 
replacereturns a URL that changes.comin the input string to.net, based on the regular expression\..*m. - 
scanreturns a subarray of comma-separated substrings that the regular expression matches. - 
splitBysplits an input string into an array of substrings based on the.in the input. 
{
  "contains": true,
  "find": [
    [
      0
    ],
    [
      4
    ],
    [
      7
    ],
    [
      12
    ]
  ],
  "match": [
    "mycompany.com",
    "mycompany"
  ],
  "matches": true,
  "replaceWith": "mycompany.net",
  "scan": [
    [
      "mycompany.com",
      "mycompany",
      "com"
    ]
  ],
  "splitBy": [
    "mycompany",
    "com"
  ]
}
For function documentation, see:
Use DataWeave Variables and Functions in a Regular Expression
This example constructs a regular expression by using the DataWeave concatenate function (++) to incorporate a DataWeave variable into a regular expression.
The regular expression matches "somebiz". The example uses replace and with to replace "somebiz" with "abcd".
%dw 2.0
var myCompany = { "name" : "biz" }
var myInputA = "somebiz-98765"
output application/json
---
{
  example: myInputA replace (("(^s.*e)" ++ myCompany.name) as Regex) with ("abcd")
}
{
  "example": "abcd-98765"
}



