---
schema_version: "1"
id: edae557912ec01f46ec606996a6978dae5822f15996212fae1c8e67cb4c431ce
title: splitBy
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.3/dw-core-functions-splitby.md
canonical_url: https://docs.mulesoft.com/dataweave/2.3/dw-core-functions-splitby
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 81f889a8497cd29beeddf4a930b4dc7438f6120313b9679e10ff79b40e1ee8c0
description: MuleSoft Documentation Site
release: "2.3"
latest: false
---
---
product: DataWeave
version: 2.3
---

# splitBy

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

## splitBy(String, Regex): Array<String>

Splits a string into a string array based on a value that matches part of that string. It filters
out the matching part from the returned array.

This version of `splitBy` accepts a Java regular expression (regex) to match the input string. The
regex can match any character in the input string. Note that `splitBy` performs the opposite
operation of `joinBy`.

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>text</code></p></td><td><p>The input string to split.</p></td></tr><tr><td><p><code>regex</code></p></td><td><p>A Java regular expression used to split the string. If it does not match some part of the string, the function will return the original, unsplit string in the array.</p></td></tr></tbody></table>

### Example

This example uses a Java regular expression to split an address block by the periods and forward
slash in it. Notice that the regular expression goes between forward slashes.

#### Source

```DataWeave
%dw 2.0
output application/json
---
"192.88.99.0/24" splitBy(/[.\/]/)
```

#### Output

```JSON
["192", "88", "99", "0", "24"]
```

### Example

This example uses several regular expressions to split input strings. The first uses `\/^*.b./\` to
split the string by `-b-`. The second uses `/\s/` to split by a space. The third example returns the
original input string in an array (`[ "no match"]`) because the regex `/^s/` (for matching the first
character if it is `s`) does not match the first character in the input string (`"no match"`). The
fourth, which uses `/^n../`, matches the first characters in `"no match"`, so it returns
`[ "", "match"]`. The last removes all numbers and capital letters from a string, leaving each of
the lower case letters in the array. Notice that the separator is omitted from the output.

#### Source

```DataWeave
%dw 2.0
output application/json
---
{ "splitters" : {
   "split1" : "a-b-c" splitBy(/^*.b./),
   "split2" : "hello world" splitBy(/\s/),
   "split3" : "no match" splitBy(/^s/),
   "split4" : "no match" splitBy(/^n../),
   "split5" : "a1b2c3d4A1B2C3D" splitBy(/^*[0-9A-Z]/)
  }
}
```

#### Output

```JSON
{
  splitters: {
    split1: [ "a", "c" ],
    split2: [ "hello", "world" ],
    split3: [ "no match" ],
    split4: [ "", "match" ],
    split5: [ "a", "b", "c", "d" ]
  }
}
```

### Example

This example splits the number by `.` and applies the index selector `[0]` to the result of the
splitBy function. The splitBy returns `["192", "88", "99", "0"]` so the index selector `[0]` just
returns the first element in the array ("192").

#### Source

```DataWeave
%dw 2.0
output application/json
---
("192.88.99.0" splitBy("."))[0]
```

#### Output

```JSON
"192"
```

### Example

This example uses a Java regular expression to split a string by `.` at every point the input string
matches the regex. Note that the regular expression does not consider the periods between the
backticks `` ` ``.

#### Source

```DataWeave
%dw 2.0
output application/json
---
'root.sources.data.`test.branch.BranchSource`.source.traits' splitBy(/[.](?=(?:[^`]*`[^`]*`)*[^`]*$)/)
```

#### Output

```JSON
[
  "root",
  "sources",
  "data",
  "`test.branch.BranchSource`",
  "source",
  "traits"
]
```

## splitBy(String, String): Array<String>

Splits a string into a string array based on a separating string that matches part of the input
string. It also filters out the matching string from the returned array.

The separator can match any character in the input. Note that `splitBy` performs the opposite
operation of `joinBy`.

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>text</code></p></td><td><p>The string to split.</p></td></tr><tr><td><p><code>separator</code></p></td><td><p>A string used to separate the input string. If it does not match some part of the string, the function will return the original, unsplit string in the array.</p></td></tr></tbody></table>

### Example

This example splits a string containing an IP address by its periods.

#### Source

```DataWeave
%dw 2.0
output application/json
---
"192.88.99.0" splitBy(".")
```

#### Output

```JSON
["192", "88", "99", "0"]
```

### Example

The first example (`splitter1`) uses a hyphen (`-`) in `"a-b-c"` to split the string. The second
uses an empty string (`""`) to split each character (including the blank space) in the string. The
third example splits based on a comma (`,`) in the input string. The last example does not split the
input because the function is case sensitive, so the upper case `NO` does not match the lower case
`no` in the input string. Notice that the separator is omitted from the output.

#### Source

```DataWeave
%dw 2.0
output application/json
---
{ "splitters" : {
    "split1" : "a-b-c" splitBy("-"),
    "split2" : "hello world" splitBy(""),
    "split3" : "first,middle,last" splitBy(","),
    "split4" : "no split" splitBy("NO")
   }
}
```

#### Output

```JSON
{
  splitters: {
    split1: [ "a","b","c" ],
    split2: [ "h","e","l","l","o","","w","o","r","l","d" ],
    split3: [ "first","middle","last"],
    split4: [ "no split"]
  }
}
```
