title: A "Nice" way to get substring between an opening and a matching closing tag (parantheses etc.)
body: <p>I quite frequently run into the task of extracting a substring from a string which is located between an opening tag (such as "(") and a closing tag (for example ")"), <strong>such that the "level" of the opening tag matches the "level" of the closing tag</strong>.</p>
  
  <p>For example, in the following string, </p>
  
  <pre><code>((a + (b * c) + d) + e)
  </code></pre>
  
  <p>when given the index 1 (pointing to the second opening tag "(" in the string), I would be interested in the substring </p>
  
  <pre><code>(a + (b * c) + d)
  </code></pre>
  
  <p>I'm NOT interested in the part until the first closing tag:</p>
  
  <pre><code>(a + (b * c)
  </code></pre>
  
  <p>In addition to one-character tags, longer strings (e.g., "begin", "end") should work, too. It is not hard to solve this, but I always find my running through the string in a loop quite ugly, possibly inefficient and error-prone.</p>
  
  <p><strong>Is there a nice way to do this?</strong> </p>
  
  <p>(Maybe there is some regex hack? But they won't work directly due to the need of counting.)</p>