title: Php regex, trying to exclude a match within another match
body: <p>I have this regex to scan for each loops in my views, it almost does what I want, but not entirely. Im using this regex atm:</p>
  
  <pre><code>(?=({{each\s+([^{}]*)}}((?&gt;(?!{{#?each).|(?1))*){{#each}}))
  </code></pre>
  
  <p>When I have an each-loop within another each-loop, it shows the matches correctly, but I don't want to inner each loop to be a match. I am planning to process this 2nd match recursively. Here is an example HTML:</p>
  
  <pre><code>&lt;div class="rows-container"&gt;
  {{each infoItems}}
      &lt;div class="row"&gt;
          &lt;div class="row-key"&gt;&lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="sub-container"&gt;
      {{each subItems}}
          &lt;div class="row"&gt;
              &lt;div class="row-key"&gt;&lt;/div&gt;
          &lt;/div&gt;
      {{#each}}
      &lt;/div&gt;
  {{#each}}
  &lt;/div&gt;
  
  &lt;div class="rows-container"&gt;
  {{each newsItems}}
      &lt;div class="row"&gt;
          &lt;div class="row-key"&gt;&lt;/div&gt;
      &lt;/div&gt;
  {{#each}}
  &lt;/div&gt;
  </code></pre>
  
  <p>Will produce the following results:</p>
  
  <pre><code>//MATCH 1:
  1. {{each infoItems}} &lt;div class="row"&gt; &lt;div class="row-key"&gt;&lt;/div&gt; &lt;/div&gt; &lt;div class="sub-container"&gt; {{each subItems}} &lt;div class="row flex-wrap"&gt; &lt;div class="row-key"&gt;&lt;/div&gt; &lt;/div&gt; {{#each}} &lt;/div&gt; {{#each}}
  2. infoItems
  3. &lt;div class="row"&gt; &lt;div class="row-key"&gt;&lt;/div&gt; &lt;/div&gt; &lt;div class="sub-container"&gt; {{each subItems}} &lt;div class="row flex-wrap"&gt; &lt;div class="row-key"&gt;&lt;/div&gt; &lt;/div&gt; {{#each}} &lt;/div&gt; 
  
  
  //MATCH 2:
  //This is the match I was trying to exclude:
  4. {{each subItems}} &lt;div class="row flex-wrap"&gt; &lt;div class="row-key"&gt;&lt;/div&gt; &lt;/div&gt; {{#each}}
  5. subItems
  6. &lt;div class="row flex-wrap"&gt; &lt;div class="row-key"&gt;&lt;/div&gt; &lt;/div&gt; 
  
  //MATCH 3:
  7. {{each newsItems}} &lt;div class="row"&gt; &lt;div class="row-key"&gt;&lt;/div&gt; &lt;/div&gt; {{#each}}
  8. newsItems
  9. &lt;div class="row"&gt; &lt;div class="row-key"&gt;&lt;/div&gt; &lt;/div&gt; 
  </code></pre>
  
  <p>As you can see all I need is: the full result (which is useful for the replace callback), the array name, &amp; the content. I have tried puzzling with the regex, </p>
  
  <pre><code>{{each\s+([^{}]*)}}((?&gt;(?!{{#?each).|(?1))*){{#each}}
  </code></pre>
  
  <p>This will only give the 2nd &amp; the third, but not the first ='( I'm starting to think I'm in over my head with regexes this complex, losing the understanding of them... but I like to push things. </p>
  
  <p>Is there a way to capture the 1st and 3rd match? with allowing other each loops inside (only 1 inside required.. for now), but not matching them. 
  A little bit of explanation or feedback will be very much appreciated, but I got some time on my hands and I am in for a puzzle. </p>