Split string in dataweave

Abhay picture Abhay · Sep 6, 2016 · Viewed 13.8k times · Source

I have a string like "Hi I am from "Kazan, Russia". The output should be

Hi
I
am
from
Kazan, Russia

I tried the regex [^\s\"']+|\"([^\"]*)\"|'([^']*)', which works fine on regexp.com. But in dataweave it does nothing. This is what I tried

%dw 1.0
%output application/json
%function split(data) {returnData: data splitBy "[^\s\"']+|\"([^\"]*)\"|'([^']*)'"}
---
split(payload)[0]

Edit 1

I tried scan operator
This is what I did

%dw 1.0
%output application/json
---
{
payload: payload scan /[^\s\"']+|\"([^\"]*)\"|'([^']*)'/
}

But I am getting NullPointerException
Stack trace

Message               : null (java.lang.NullPointerException).
Payload               :      com.mulesoft.weave.reader.DefaultSeekableStream@5d334a28
Element               : /logFlow/processors/0 @ log:log.xml:14 (Transform Message)
Element XML           : <dw:transform-message doc:name="Transform Message" metadata:id="87d4353c-240d-4b1c-84b4-171f6c11045b">
                    <dw:input-payload mimeType="plain/text"></dw:input-payload>
                    <dw:set-payload>%dw 1.0%output application/json---{payload: payload scan /[^\s\"']+|\"([^\"]*)\"|'([^']*)'/}</dw:set-payload>
                    </dw:transform-message>

Answer

sulthony h picture sulthony h · Sep 7, 2016

Referring DataWeave documentation, Regular Expressions are defined between /. Therefore, replace the double quote " with /. So it should be like this: /[^\s\"']+|\"([^\"]*)\"|'([^']*)'/

Another thing is, splitBy splits a string into an array of separate elements. So you will not get the expected result. Indeed, the payload will be split to 5 elements, but its content is empty. For example: "a,b,c" splitBy "," returns 3 elements: "a" - "b" - "c", the comma/separator is not part of the result.

To get the expected result, try to use scan. It returns an array with all of the matches in the given string. Might be you should modify the regex accordingly.