title: Regex - Recursion - nested matches with multiple ending
body: <p>this is my first question on stackoverflow so please bare with me here. Also I am not a native english speaker.</p>
  <p>(16.02.2022)
  <strong>ANSWER</strong> (<a href="https://regex101.com/r/4FRznK/1" rel="nofollow noreferrer">https://regex101.com/r/4FRznK/1</a> from Comment on Answer). Special thanks to Casimir et Hippolyte for your help! I wish I could reach out to you.</p>
  <pre><code>\[if \s+ (?&lt;cond&gt; [^]]* ) ]
  
  (?&lt;content&gt; [^[]*+ 
          (?: (?R) [^[]*
            | \[ (?! /if] | else (?:if)?  \b) [^[]*
          )*+
  )
  (?&lt;rest&gt; 
      (?: \[elseif \s+ [^]]* ] \g&lt;content&gt; )*+
      (?: \[else] \g&lt;content&gt; )?+
      \[/if]
  )
  </code></pre>
  <p>(15.02.2022) <strong>UPDATE:</strong>
  I fiddled around with the solutions presented below and have gotten farther with it. Seems like there is a limit on string length to comfortably match without any catastophic backtracking.</p>
  <p>I have updated my Regex101 to show the recent progress. Maybe one of you has an idea on how to tackle this. <a href="https://regex101.com/r/wYzA3e/4" rel="nofollow noreferrer">https://regex101.com/r/wYzA3e/4</a></p>
  <p><strong>SIDE NOTE:</strong>
  I do have a working function for this, but my goal is to find a faster solution in terms of optimization and reliability. My current functions takes (in my opinion) way to long to complete the task and relies havily on strpos to get me there. I do not actually want to use third-party functions if there is a cheaper (in terms of performance) solution with PHPs internal functions.
  So even if you advice me to use alternative approaches, please be kind and provide hints on which explicitly you mean by these. Thank you!</p>
  <p>(14.02.2022) <strong>Original:</strong>
  I am presented with the following difficulties with my regular expression:
  This is the string (&quot;true&quot; and/or &quot;false&quot; are not actually in the string but it helps with simplification):</p>
  <pre><code>**[if true]**
      [if true]
          [if false]
          [else]
          [/if]
      *[elseif false]*
          [if true]
          [/if]
      [else]
      [/if]
  **[elseif false]**
  [/if]
  **[if false]**
      [if false]
      [else]
      *[/if]*
  **[else]**
      [if true]
      [/if]
  [/if]
  </code></pre>
  <p>I marked the <strong>wanted matches</strong> (**) and the <em>ones i got</em> (*)</p>
  <p>In this situation I do only want to match the most outer parent [if XXXX].([else]|[elseif XXX]|[/if]) statement with its according end which can be [else], [elseif XXX] or [/if]. For now i do not care about the inner [if XXX] since when the parent is false i dont need to check for them.</p>
  <p>When running my regex:</p>
  <pre><code>/\[if (.*?)\](((?R)|.)*?)(\[\/if\]|\[else\]|\[elseif )/gs 
  </code></pre>
  <p>it matches the parents [if XXX] and an incoherent combination of any [elseif XX], [else], [/if] in it.</p>
  <p>As groups I do need the match &gt; every X [if XXX] &gt; the content between [if XXX] and the matching [END] as well as the [END].</p>
  <p>Since i do not fully understand Recursion I´d appreciate your help. Many thanks in advance!</p>
  <p>You can try the regex here (updated):
  <a href="https://regex101.com/r/wYzA3e/4" rel="nofollow noreferrer">https://regex101.com/r/wYzA3e/4</a></p>