Split string delimited by comma without respect to commas in brackets

后端 未结 3 1936
情话喂你
情话喂你 2020-12-11 22:36

I\'ve got a string like

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

and I want to split it int

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-11 22:59

    Assuming ( and ) are not nested and unescaped. You can use split using:

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

    RegEx Demo

    ,(?![^()]*\)) will match a comma if it is NOT followed by a non-parentheses text and ), thus ignoring commas inside ( and ).

提交回复
热议问题