4

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"]
John Diaz
  • 341
  • 3
  • 13
  • 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 Dec 11 '19 at 18:29

1 Answers1

5

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"}
oim
  • 1,141
  • 10
  • 14