0

I want to split a string by '|' delimiter and exclude those inside the brackets (). I used tokenize function to split by delimiter and have some issue with exclusion part(RegEx format). Please help.

Input: Test|Test1|Test2|(Test3|Test4)|Test5|(Test6)(Test7)|Test8

Output: Test,Test1,Test2,(Test3|Test4),Test5,(Test6)(Test7),Test8

Thanks in advance.

Learner
  • 5
  • 1

1 Answers1

1

Perhaps the analyze-string function suffices to break up the string, I don't think the single example makes the rules clear but it would be like

analyze-string('Test|Test1|Test2|(Test3|Test4)|Test5|(Test6)(Test7)|Test8','(\([^)]*\))+')/*
/(if (. instance of element(fn:match)) then data() else tokenize(., '\|'))[normalize-space()]

either => string-join(',') that result or use

declare option output:method 'text';
declare option output:item-separator ',';

analyze-string('Test|Test1|Test2|(Test3|Test4)|Test5|(Test6)(Test7)|Test8','(\([^)]*\))+')/*
/(if (. instance of element(fn:match)) then data() else tokenize(., '\|'))[normalize-space()]
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Appreciate the prompt response @Martin. It works perfect. But why do I need to use analyze string function and if-else, In Java, it's staight foward with simple RegEx. Just trying to understand a bit on Xquery. – Learner Jun 22 '21 at 18:14
  • Well, try your regular expression with XQuery or at least show the Java code you have if you expect a comparison of two languages. – Martin Honnen Jun 22 '21 at 18:26