java - Split string delimited by comma without respect to commas in brackets -


i've got string like

s="abc, 3rncd (23uh, sdfuh), 32h(q23q)89 (as), dwe8h, edt (1,wer,345,rtz,tr t), nope"; 

and want split string

string[] parts={"abc", "3rncd (23uh, sdfuh)", "32h(q23q)89 (as)", "dwe8h", "edt (1,wer,345,rtz,tr t)", "nope"}; 

if call s.split(",") after trimming different result because in of string, example "3rncd (23uh, sdfuh)" there still comma. don't want commas in brackets. there elegant way solve problem?

assuming ( , ) not nested , unescaped. can use split using:

string[] arr = input.split(",(?![^()]*\\))\\s*"); 

regex demo

,(?![^()]*\)) match comma if not followed non-parentheses text , ), ignoring commas inside ( , ).


Comments

Popular posts from this blog

Capture and play voice with Asterisk ARI -

c++ - Can not find the "fiostream.h" file -

java - Why database contraints in HSQLDB are only checked during a commit when using transactions in Hibernate? -