title: How to get all captures of subgroup matches with preg_match_all()?
body: <p><strong>Update/Note:</strong></p>
  
  <blockquote>
    <p>I think what I'm probably looking for is to get the <a href="https://stackoverflow.com/questions/6571106/can-you-retrieve-multiple-regex-matches-in-javascript/6571139#6571139">captures of a group</a> in PHP.</p>
  </blockquote>
  
  <p>Referenced: <a href="https://stackoverflow.com/questions/4941259/pcre-regular-expressions-using-named-pattern-subroutines">PCRE regular expressions using named pattern subroutines.</a></p>
  
  <p>(Read carefully:)</p>
  
  <hr>
  
  <p>I have a string that contains a variable number of segments (simplified):</p>
  
  <pre><code>$subject = 'AA BB DD '; // could be 'AA BB DD CC EE ' as well
  </code></pre>
  
  <p>I would like now to match the segments and return them via the matches array:</p>
  
  <pre><code>$pattern = '/^(([a-z]+) )+$/i';
  $result = preg_match_all($pattern, $subject, $matches);
  </code></pre>
  
  <p>This will only return the <em>last</em> match for the capture group 2: <code>DD</code>.</p>
  
  <p>Is there a way that I can retrieve all subpattern captures (<code>AA</code>, <code>BB</code>, <code>DD</code>) with one regex execution? Isn't <code>preg_match_all</code> suitable for this?</p>
  
  <h2>This question is a generalization.</h2>
  
  <p>Both the <code>$subject</code> and <code>$pattern</code> are simplified. Naturally with such the general list of <code>AA</code>, <code>BB</code>, .. is much more easy to extract with other functions (e.g. <code>explode</code>) or with a variation of the <code>$pattern</code>.</p>
  
  <p>But I'm specifically asking how to return all of the subgroup matches with the <code>preg_...</code>-family of functions.</p>
  
  <p>For a real life case imagine you have multiple (nested) level of a variant amount of subpattern matches.</p>
  
  <h2>Example</h2>
  
  <p>This is an example in pseudo code to describe a bit of the background. <em>Imagine</em> the following:</p>
  
  <p>Regular definitions of tokens:</p>
  
  <pre><code>   CHARS := [a-z]+
     PUNCT := [.,!?]
     WS := [ ]
  </code></pre>
  
  <p><code>$subject</code> get's tokenized based on these. The tokenization is stored inside an array of tokens (type, offset, ...).</p>
  
  <p>That array is then transformed into a string, containing one character per token:</p>
  
  <pre><code>   CHARS -&gt; "c"
     PUNCT -&gt; "p"
     WS -&gt; "s"
  </code></pre>
  
  <p>So that it's now possible to run regular expressions based on tokens (and not character classes etc.) on the token stream string index. E.g.</p>
  
  <pre><code>   regex: (cs)?cp
  </code></pre>
  
  <p>to express one or more group of chars followed by a punctuation.</p>
  
  <p>As I now can express self-defined tokens as regex, the next step was to build the grammar. This is only an example, this is sort of ABNF style:</p>
  
  <pre><code>   words = word | (word space)+ word
     word = CHARS+
     space = WS
     punctuation = PUNCT
  </code></pre>
  
  <p>If I now compile the grammar for <em>words</em> into a (token) regex I would like to have naturally all subgroup matches of each <em>word</em>.</p>
  
  <pre><code>  words = (CHARS+) | ( (CHARS+) WS )+ (CHARS+)    # words resolved to tokens
    words = (c+)|((c+)s)+c+                         # words resolved to regex
  </code></pre>
  
  <p>I could code until this point. Then I ran into the problem that the sub-group matches did only contain their last match.</p>
  
  <p>So I have the option to either create an automata for the grammar on my own (which I would like to prevent to keep the grammar expressions generic) or to somewhat make preg_match working for me somehow so I can spare that.</p>
  
  <p>That's basically all. Probably now it's understandable why I simplified the question.</p>
  
  <hr>
  
  <p>Related:</p>
  
  <ul>
  <li><a href="http://regexkit.sourceforge.net/Documentation/pcre/pcrepattern.html" rel="nofollow noreferrer">pcrepattern man page</a></li>
  <li><a href="https://stackoverflow.com/questions/6579908/get-repeated-matches-with-preg-match-all">Get repeated matches with preg_match_all()</a></li>
  </ul>