3

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>
Abhay
  • 314
  • 1
  • 2
  • 11

5 Answers5

1

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.

sulthony h
  • 1,279
  • 1
  • 8
  • 9
1

I am not sure about the syntax you have used in your initial query, but the following should work for the payloads with the following formats:

Hello I am from "Kazan, Russia" 

and:

Hello I am from "Kazan, Russia

The below sample uses:

  1. Possessive Quantifiers that match on the required word characters.

  2. An explicit match on , to help match the location.

  3. Reduce the output from an array.

DataWeave:

%dw 1.0
%input payload application/json
%output application/json
%var data =  "Hello I am from \"StackOverflow, Internet\""
---
data scan /\w++, \w++|\w++/ reduce ($$ ++ $)
Matt Jones
  • 405
  • 2
  • 11
  • Can you please explain the regex part, what `\w++, \w++|w++` does? I know `\w` will match non word character but not getting the rest. – Abhay Apr 28 '17 at 05:32
1

It works for me:

%dw 2.0

%output application/json

payload map ((payload01 , indexOfPayload01) -> { accountType: payload01.accountType, address: payload01.street ++ payload01.city, country: payload01.country, creationDate: payload01.creationDate as :string, firstName: (payload01.name splitBy ' ')[0], id: payload01.accountID, lastName: (payload01.name splitBy ' ')[1], miles: payload01.miles, postal: payload01.postal })

Md. Kamruzzaman
  • 1,895
  • 16
  • 26
0

Use this expression

%dw 1.0 %output application/json %var splittedValue='Hi I am from Kazan, Russia' //splitBy " "


splittedValue splitBy " "

Zahid
  • 59
  • 10
0

I achieved this in dataweave by my making a userdefine function that calls splitBy() function In the code below I'm parsing the name return from db in firstname and last name

%dw 2.0
output application/json
fun split(data) = data splitBy (" ")
---
payload map ( payload01 , indexOfPayload01 ) -> {
    id: payload01.accountID default "",
    address: ((payload01.street default "") ++ (payload01.city default "")) ++ (payload01.state default ""),
    postal: payload01.postal default "",
    country: payload01.country default "",
    creationDate: payload01.creationDate as String default "",
    accountType: payload01.accountType default "",
    firstName: split(payload01.name)[0],
    lastName: split(payload01.name)[1],
    miles: payload01.miles default 0
}
mudassir ahmed
  • 191
  • 1
  • 1
  • 13