title: PCRE regular expressions using named pattern subroutines
body: <p>I am experimenting with the named subpattern/'subroutine' regex features in PHP's PCRE and I'm hoping someone can explain the following strange output:</p>
  
  <pre><code>$re = "/
  (?(DEFINE)
      (?&lt;a&gt; a )
  )
  
  ^(?&amp;a)$
  
  /x";
  
  var_dump(preg_match($re, 'a', $match)); // (int) 1 as expected
  var_dump($match); // Array( [0] =&gt; 'a' ) &lt;-- Why?
  </code></pre>
  
  <p>I can't understand why the named group "a" is not in the result (with the contents "a").  Changing <code>preg_match</code> to <code>preg_match_all</code> puts "a" and "1" in the match data but both contain only an empty string.</p>
  
  <p>I really like the idea of writing regular expressions this way, as you can make them incredibly powerful whilst keeping them very maintainable (see <a href="https://stackoverflow.com/questions/764247/why-are-regular-expressions-so-controversial/4053506#4053506">this answer</a> for a good example of this), however if the subpatterns are not available in the match data then it's not much use really.</p>
  
  <p>Am I missing something here or should I just mourn what could have been and move on?</p>