splitBy Examples in DataWeave 2.0

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
%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
{
  "splitters": {
    "split1": [
      "a",
      "c"
    ],
    "split2": [
      "hello",
      "world"
    ],
    "split3": [
      "no match"
    ],
    "split4": [
      "",
      "match"
    ],
    "split5": [
      "a",
      "b",
      "c",
      "d"
    ]
  }
}

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
%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
{
  "splitters": {
    "split1": [
      "a",
      "b",
      "c"
    ],
    "split2": [
      "h",
      "e",
      "l",
      "l",
      "o",
      " ",
      "w",
      "o",
      "r",
      "l",
      "d"
    ],
    "split3": [
      "first",
      "middle",
      "last"
    ],
    "split4": [
      "no split"
    ]
  }
}

Input
"Manish Kumar"

Dataweave 2.0 Expression

%dw 2.0
output application/java
---
payload splitBy(" ")

Output
[Manish, Kumar]

plitBy(text: Null, separator: Any)

Helper function that enables splitBy to work with a null value.

Recent Posts

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

19 hours ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 weeks ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

1 month ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 month ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

2 months ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

2 months ago