title: Recursive regex in PHP
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags">RegEx match open tags except XHTML self-contained tags</a>  </p>
  </blockquote>
  
  
  
  <p>Hi to all!</p>
  
  <p>Could you please explain to me a little about a recursive regular expression in PHP PCRE? So, for example I have general nested html:</p>
  
  <pre><code>&lt;div id="first"&gt;
  
    &lt;b&gt;some text&lt;/b&gt;
  
      &lt;div id="second"&gt;
  
        &lt;div id="third"&gt;
  
          ...
  
        &lt;/div&gt;
  
      &lt;/div&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p>and want to build an array of divs. According to docs, I can call to regex in two ways: (?R) or (?n). In last example n references to appopriate subpattern. So, when I call (?R) does it start to search string with a whole expression from the very beginning and "forgets" about first match? Logically, I can describe a pattern to above string like this: 'search div, which will contain zero or more attributes, and which may contain nested divs. Stop if there are no more nested divs inside."</p>
  
  <p>I have tried with it a little, but often got an error: 'compilation error, regex can loop indefinitely."</p>
  
  <p>I would like to understand the theory how recursions work in regular expression.</p>