4
votes

i'm trying to split a dot notated string using splitBy in dataweave 2.0, using a simple regex to avoid splitting escaped strings, testing online the regex works as expected but the dataweave function outputs a different result.

'root.sources.data.`jenkins.branch.BranchSource`.source.traits' splitBy(/[^\.`]+|"([^`]*)"|`([^`]*)`/)

Output:

["", ".", ".", ".", ".", "." ]

Expected:

["root", "sources", "data", "`jenkins.branch.BranchSource`", "source, "traits"]
1
The splitBy need a RegEx that matches the separator (the subString that separates the parts), but that RegEx is matching the parts that you want as output.Christian Chibana

1 Answers

5
votes

You can try using lookahead to check existence or non existence of `

'root.sources.data.`jenkins.branch.BranchSource`.source.traits' splitBy(/[.](?=(?:[^`]*`[^`]*`)*[^`]*$)/)

this will result to

[
  "root" as String {class: "java.lang.String"}, 
  "sources" as String {class: "java.lang.String"}, 
  "data" as String {class: "java.lang.String"}, 
  "`jenkins.branch.BranchSource`" as String {class: "java.lang.String"}, 
  "source" as String {class: "java.lang.String"}, 
  "traits" as String {class: "java.lang.String"}
] as Array {encoding: "UTF-8", mediaType: "*/*", mimeType: "*/*", class: "java.util.ArrayList"}