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

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -