---
schema_version: "1"
id: e46f8c600ea92d4649cb8342bcd4647c3029760fe2875d0879ff654767ac5c6c
title: Use Regular Expressions in DataWeave
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.11/dataweave-cookbook-use-regex.md
canonical_url: https://docs.mulesoft.com/dataweave/2.11/dataweave-cookbook-use-regex
source_site: docs.mulesoft.com
content_type: documentation
content_hash: f1c00cdfd1aa2fa5a47fa3a8a837fcb84f1e176813d095ba8f9e935fd9ec97eb
description: MuleSoft Documentation Site
release: "2.11"
latest: false
---
---
product: DataWeave
version: 2.11
keywords: studio, anypoint, transform, transformer, format, aggregate, rename, split, filter convert, json, metadata, dataweave, data weave, datamapper, dwl, dfl, dw, output structure, input structure, map, mapping, as
---

# Use Regular Expressions in DataWeave

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

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".

- `contains` returns `true` based on a regular expression that matches part of the input string.

- `find` returns an array of indices that specify the matching locations in the input string. This
  function treats the input string as a string array.

- `match` returns an array of substrings that match the regular expression.

- `matches` returns `true` because the regular expression matches the input string exactly.

- `replace` returns a URL that changes `.com` in the input string to `.net`, based on the regular
  expression `\..*m`.

- `scan` returns a subarray of comma-separated substrings that the regular expression matches.

- `splitBy` splits an input string into an array of substrings based on the `.` in the input.

DataWeave Script:

```dataweave
%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(/[.\/]/)
}
```

Output JSON:

```json
{
  "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:

- [contains](https://docs.mulesoft.com/dataweave/2.11/dw-core-functions-contains)

- [find](https://docs.mulesoft.com/dataweave/2.11/dw-core-functions-find)

- [match](https://docs.mulesoft.com/dataweave/2.11/dw-core-functions-match)

- [matches](https://docs.mulesoft.com/dataweave/2.11/dw-core-functions-matches)

- [replace](https://docs.mulesoft.com/dataweave/2.11/dw-core-functions-replace)

- [scan](https://docs.mulesoft.com/dataweave/2.11/dw-core-functions-scan)

- [splitby](https://docs.mulesoft.com/dataweave/2.11/dw-core-functions-splitby)

## 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".

DataWeave Script:

```dataweave
%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")
}
```

Output JSON:

```json
{
  "example": "abcd-98765"
}
```

## See Also

- [Regex (dw::Core Type)](https://docs.mulesoft.com/dataweave/2.11/dataweave-types#dw_type_regex)

- [Core Types (dw::Core)](https://docs.mulesoft.com/dataweave/2.11/dw-core-types)

- [DataWeave Selectors](https://docs.mulesoft.com/dataweave/2.11/dataweave-selectors)

- [DataWeave Cookbook](https://docs.mulesoft.com/dataweave/2.11/dataweave-cookbook)
