In general, when you are working on a problem and you think &quot;let me use a regex for that&quot; and then you come up with<p><pre><code>    ^\s*[^\s()}]+(?&lt;m&gt;[^()]*\((?:\g&lt;m&gt;|[^()]*)\)[^()]*)*[^()]*\)[,]?$
</code></pre>
to solve your problem, then you have IMHO come across a problem which you should not be solving using regular expressions.<p>Case in point is counting and balancing parentheses which is very easily done using a single loop over the string in question.