title: Using regex to parse nested IF statements
body: <p>I am working on my very own templating engine in the name of learning and hobbies. I have a regex that looks for if statements using a syntax almost identical to TWIG.</p>
  
  <p>You can view the regex <a href="https://regex101.com/r/wL5cW6/1" rel="nofollow">here</a> with a few working examples and then one I am trying to make work.</p>
  
  <p>Here is the regex:</p>
  
  <pre><code>{%\s*if\s+(?&lt;var&gt;(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(?:\.(?:[a-zA-Z0-9_\x7f-\xff]*))*)\s(?:(?&lt;operation&gt;=|!=|&lt;=|&lt;|&gt;=|&gt;)\s(?&lt;var2&gt;(?:(?:(?1)*)(?:\.(?:(?2)*))*)|(?:[0-9]+))\s?)?%}(?&lt;if&gt;(?:(?!{% e(?:lse|nd if) %}).)*)(?:{%\h?else\h?%}(?&lt;else&gt;[\s\S]*?))?{%\h?end if\s?%}
  </code></pre>
  
  <p>And here is the data it's processing:</p>
  
  <pre><code>THESE WORK
          {% if thing %}
          stuff
          {% end if %}
  
          {% if thing %}
          stuff
          {% else %}
          other stuff
          {% end if %}
  
          {%if thing = thingy %}
          stuff
          {% else %}
          other stuff
          {% end if %}
  THIS DOESN'T
          Problem starts here:
          {% if this = that %}
          {% if item.currency = 0 %}
          selected="selected"
          {% else %}
          you
          {% end if %}
          {% end if %}
  </code></pre>
  
  <p>Basically I would like the regex to search for the last {% end if %} tag and use everything in between as a string that I can recursively parse later.</p>
  
  <p>Also, as a side note, is it appropriate to leave most of the question's information in a link to regex tester? Or should I also copy the bulk of the question in here (on SO)?</p>