Split string by comma if not within square brackets or parentheses
<p>I have a long string, and I want to set it to an array by splitting it by the comma as long as the comma is not within square brackets or parentheses. I've tried a couple of variations but not getting exactly what I'm looking for...</p>



<p>Example 1:</p>

<pre class="lang-none prettyprint-override"><code>Harry Potter, Hermione, (Severus, Snape)
Returns:
Harry Potter
Hermione
Severus, Snape
</code></pre>

<p>Example 2:</p>

<pre class="lang-none prettyprint-override"><code>Harry Potter, [and, the chamber, of secrets], Hermione, (Olivanders, Wands)
Returns:
Harry Potter
and, the chamber, of secrets
Hermione
Olivanders, Wands
</code></pre>