LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to split string into array while ignoring portion in parentheses?

I have string like this: token1; token2, (token3, token4), {token5; token6}.

I would like to split it into array by comma or semicolon as a delimiters, but I do not want to split portion in any sort of parentheses (like {}, [], (), <>).

So, my output array should have four items:

[0]: token1

[1]: token2

[2]: (token3, token4)

[3]: {token5; token6}

"Scan String For Tokens" or similar approaches perfectly do the job without considering parentheses, but I really need it to keep parentheses together within the same output token...

 

Are there any good ideas?

0 Kudos
Message 1 of 4
(896 Views)

Have you considered first preprocessing the string through the "Match Pattern" function to pull out all instances of strings bounded by parentheses?  It's easy enough to do if you don't mind if the order of the elements in the final array is not important. It gets a bit tougher if you want to maintain the same order as found in the string.

0 Kudos
Message 2 of 4
(879 Views)

PCRE is a beast. No preprocessing or any other complicated stuff is needed. A not-so-simple regular expression will do the trick.

 

My quick solution is

 

"[;,](?=(?:(?:[^\(\)\{\}\[\]]*+(?<!\\)[\(\)\{\}\[\]]){2})*+[^\(\)\{\}\[\]]*+\Z)"

(without the quotation marks, of course) 

 

snip.png

Some work may needed. You have to test it with real data.

 

0 Kudos
Message 3 of 4
(812 Views)

If performance and code readability is important (regex are typically slow!), I would just rattle trough the bytes as follows:

 

altenbach_0-1700336938813.png

 

Of course if your brackets are not well behaved (i.e. not alternating in on/off function) more code is needed. Not sure if you also want to trim whitespace from the entries. I am sure it needs a bit finetuning.

 

 

0 Kudos
Message 4 of 4
(794 Views)