<p>我想加上这个答案作为快速参考。随时更新。</p>
<hr/>
<p><strong>.NET Regex</strong>使用<a href="http://www.regular-expressions.info/balancing.html" rel="noreferrer">balancing groups</a>。</p>
<pre><code>\((?>\((?<c>)|[^()]+|\)(?<-c>))*(?(c)(?!))\)
</code></pre>
<p>其中<code>c</code>用作深度计数器。</p>
<p><a href="http://regexstorm.net/tester?p=%5C((%3F%3E%5C((%3F%3Cc%3E)%7C%5B%5E()%5D%2B%7C%5C)(%3F%3C-c%3E))*(%3F(c)(%3F!))%5C)&i=some%20text(text%20here(possible%20text)text(possible%20text(more%20text)))end%20text" rel="noreferrer">Demo at Regexstorm.com</a></p>
<ul>
<li><a href="https://stackoverflow.com/questions/7898310/using-regex-to-balance-match-parenthesis"><strong>Stack Overflow</strong>: Using RegEx to balance match parenthesis</a></li>
<li><a href="http://weblogs.asp.net/whaggard/377025" rel="noreferrer"><strong>Wes' Puzzling Blog</strong>: Matching Balanced Constructs with .NET Regular Expressions</a></li>
<li><a href="http://www.rassoc.com/gregr/weblog/2003/05/15/nested-constructs-in-regular-expressions/" rel="noreferrer"><strong>Greg Reinacker's Weblog</strong>: Nested Constructs in Regular Expressions</a></li>
</ul>
<hr/>
<p><strong>PCRE</strong>使用<a href="http://php.net/manual/en/regexp.reference.recursive.php" rel="noreferrer">recursive pattern</a>。</p>
<pre><code>\((?:[^)(]+|(?R))*+\)
</code></pre>
<p><a href="https://regex101.com/r/eBtSTM/1" rel="noreferrer">Demo at regex101</a>;或无替换:</p>
<pre><code>\((?:[^)(]*(?R)?)*+\)
</code></pre>
<p><a href="https://regex101.com/r/eBtSTM/2" rel="noreferrer">Demo at regex101</a>;或<a href="http://www.softec.lu/site/RegularExpressions/UnrollingTheLoop" rel="noreferrer">unrolled</a>用于性能:</p>
<pre><code>\([^)(]*+(?:(?R)[^)(]*)*+\)
</code></pre>
<p><a href="https://regex101.com/r/eBtSTM/3" rel="noreferrer">Demo at regex101</a>;模式粘贴在代表<code>(?0)</code>的<code>(?R)</code>处。</p>
<p><em>Perl、PHP、记事本++、</em><em>R</em>:<a href="http://www.regular-expressions.info/rlanguage.html" rel="noreferrer">perl=TRUE</a>、<em>Python</em>:<a href="https://pypi.python.org/pypi/regex" rel="noreferrer">Regex package</a>和<code>(?V1)</code>用于Perl行为。</p>
<hr/>
<p><strong>Ruby</strong>使用<a href="http://ruby-doc.org/core-2.0.0/Regexp.html#class-Regexp-label-Subexpression+Calls" rel="noreferrer">subexpression calls</a>。</p>
<p>使用Ruby 2.0 <code>\g<0></code>可以调用完整模式。</p>
<pre><code>\((?>[^)(]+|\g<0>)*\)
</code></pre>
<p><a href="http://rubular.com/r/RUDsEv7SBZ" rel="noreferrer">Demo at Rubular</a>;Ruby 1.9只支持<a href="http://www.regular-expressions.info/subroutine.html" rel="noreferrer">capturing group recursion</a>:</p>
<pre><code>(\((?>[^)(]+|\g<1>)*\))
</code></pre>
<p><a href="http://rubular.com/r/vuyejEfjih" rel="noreferrer">Demo at Rubular</a>(自Ruby 1.9.3以来的<a href="http://ruby-doc.org/core-1.9.3/Regexp.html#class-Regexp-label-Atomic+Grouping" rel="noreferrer">atomic grouping</a>)</p>
<hr/>
<p><strong>JavaScript</strong><a href="http://xregexp.com/api/#matchRecursive" rel="noreferrer">API :: XRegExp.matchRecursive</a></p>
<pre><code>XRegExp.matchRecursive(str, '\\(', '\\)', 'g');
</code></pre>
<p>JS、Java和其他regex风格,无递归,多达2级嵌套:</p>
<pre><code>\((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)
</code></pre>
<p><a href="https://regex101.com/r/eE6mX3/1" rel="noreferrer">Demo at regex101</a>。深入<a href="https://stackoverflow.com/a/17759264">nesting needs to be added</a>到模式。<br/>
在不平衡的括号中更快地失败<a href="https://regex101.com/r/cU7uG9/1" rel="noreferrer">drop the ^{<cd6>} quantifier.</a></p>
<hr/>
<p><strong>Java</strong>:一个有趣的<a href="https://stackoverflow.com/a/47162099/5527985">idea using forward references by @jaytea</a>。</p>
<hr/>
<p><sub><a href="https://stackoverflow.com/a/22944075/5527985">Reference - What does this regex mean?</a></sub><br/></p>
<ul>
<li><a href="http://www.rexegg.com/regex-recursion.html" rel="noreferrer"><strong>rexegg.com</strong> - Recursive Regular Expressions</a></li>
<li><a href="http://www.regular-expressions.info/recurse.html" rel="noreferrer"><strong>Regular-Expressions.info</strong> - Regular Expression Recursion</a></li>
</ul>