title: What is the regular expression to match nested square bracket tags?
body: <p>I created a regular expression pattern that matches square bracket, Wiki-type tags like the following: </p>
  
  <pre class="lang-none prettyprint-override"><code>[h1]Some content[/h1]
  [b]some more content[/b]
  [i]some more still[/i]
  </code></pre>
  
  <p>Here is a scenario:</p>
  
  <pre class="lang-none prettyprint-override"><code>This [b]sentence[/b] is just an [b][i]example[/i][/b].
  </code></pre>
  
  <p>Here is the pattern:</p>
  
  <pre class="lang-regex prettyprint-override"><code>\[\w{1,2}\](.*?)\[\/\w{1,2}]
  </code></pre>
  
  <p>The thing is, sometimes the tags are nested. For example:</p>
  
  <pre class="lang-none prettyprint-override"><code>[b][i]nested tags content[/i][/b]
  </code></pre>
  
  <p>Nesting doesn't get more complicated than this. As would be expected, the pattern returns:</p>
  
  <pre class="lang-none prettyprint-override"><code>[b][i]nested tags content[/i]
  </code></pre>
  
  <p>What modification should I make in the pattern or what other pattern should I use for the match to capture the entire nested set?</p>