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*"); ,(?![^()]*\)) match comma if not followed non-parentheses text , ), ignoring commas inside ( , ).
Comments
Post a Comment