title: Regex pattern for shortcodes in PHP
body: <p>I have a problem with a regex I wrote to match shortcodes in PHP.</p>
  
  <p>This is the pattern, where <code>$shortcode</code> is the name of the shortcode:</p>
  
  <pre><code>\[$shortcode(.+?)?\](?:(.+?)?\[\/$shortcode\])?
  </code></pre>
  
  <p>Now, this regex behaves pretty much fine with these formats:</p>
  
  <ul>
  <li><code>[shortcode]</code></li>
  <li><code>[shortcode=value]</code></li>
  <li><code>[shortcode key=value]</code></li>
  <li><code>[shortcode=value]Text[/shortcode]</code></li>
  <li><code>[shortcode key1=value1 key2=value2]Text[shortcode]</code></li>
  </ul>
  
  <p>But it seems to have problems with the most common format,</p>
  
  <ul>
  <li><code>[shortcode]Text[/shortcode]</code></li>
  </ul>
  
  <p>which returns as matches the following:</p>
  
  <pre><code>Array
  (
      [0] =&gt; [shortcode]Text[/shortcode]
      [1] =&gt; ]Text[/shortcode
  )
  </code></pre>
  
  <p>As you can see, the second match (which should be the text, as the first is optional) includes the end of the opening tag and all the closing tag but the last bracket.</p>
  
  <p><strong>EDIT:</strong> Found out that the match returned is <em>the first capture</em>, not the second. <a href="http://regexr.com?31euk" rel="noreferrer">See the regex in Regexr.</a></p>
  
  <p>Can you help with this please? I'm really crushing my head on this one.</p>