title: Matching if/elseif/else statement with regular expression
body: <p>I have been writing a regular expression that allows me to match if/elseif/else statements. I've got as far as I have (matching an if/else statement) with help from the Stack Overflow community.</p>
  
  <p>This is the content I'm trying to match:</p>
  
  <pre><code>{?if gender = male}You're a guy!{?elseif gender = female}You're a gal!{?else}{#gender|capitalise}{?endif}
  </code></pre>
  
  <p>(The solution I'm aiming for would allow unlimited "elseif" statements, preferably.) It also needs to be able to match the following, to make sure it is backwards compatible:</p>
  
  <pre><code>{?if gender = male}Male{?else}Female{?endif}
  {?if gender != female}{#gender}{?endif}
  </code></pre>
  
  <p>The output I'm looking for:</p>
  
  <pre><code>0: gender = male
  1: You're a guy!
  2: {?elseif gender = female}You're a gal!
  3: {#gender|capitalise}
  </code></pre>
  
  <p>Number 2 should have every elseif in the same string, to allow them to be split and processed after, like:</p>
  
  <pre><code>2: {?elseif gender = female}You're a gal!{?elseif this = that}Output (...)
  </code></pre>
  
  <p>My current regex is close, but not good enough.</p>
  
  <pre><code>{\?if ([^{}]+)}(.+?)(?:{\?else}(.+?))?{\?endif}
  </code></pre>
  
  <p>I'm still learning Regular Expressions, so I'm not sure how to do this.</p>