I think you're going to have to go through the string letter by letter. You tack on each letter into a buffer. Then you keep track of how many left-brackets you've seen after the first one. Every time you come across a comma, you save the previous portion of the string to your collection array, UNLESS you've come across at least one left-parenthesis. In that case, you tack on the comma. Each time you come across a left-bracket, you increment the bracket counter, and each time you come across a right-bracket, you decrement the bracket counter. When the bracket counter get's back down to zero (or in this case, 1), you then go back to the "save previous string" on comma behavior.
Oh, all along while doing this, when you save previous string, you also emit a "_" to the return value / output / post-replaced string, also emitting commas and the like.
At least that's the description of the algorithm, if you can follow it, it should be fairly straightforward.