title: Regex & PHPUnit assertion : count how many times word matches in repeating pattern
body: <p>I'm using PHPUnit to test the output of a PHP function that produces HTML <code>option</code> tags using provided data.</p>
  
  <p>For example, this is a correct output of the function (I added line returns to improve readability on SO):</p>
  
  <pre><code>&lt;option value="0"&gt;Choose one ...&lt;/option&gt;
  &lt;option value="fooID"&gt;fooValue&lt;/option&gt;
  &lt;option value="barID"selected&gt;barValue&lt;/option&gt;
  &lt;option value="bazID"&gt;bazValue&lt;/option&gt;
  </code></pre>
  
  <p>And to test the output, I'm using this assertion:</p>
  
  <pre><code>$this-&gt;assertRegExp("/(&lt;option value=\".*\" *(selected)? *&gt;.*&lt;\/option&gt;)+/i", $res);
  </code></pre>
  
  <p>where <code>$res</code> is the output string of the tested function. </p>
  
  <p>And it works well. But I would like to also check that <code>selected</code> is generated in only one <code>option</code> tag.</p>
  
  <p>How can I do this? Is there a way to count how many times <code>selected</code> was matched?</p>
  
  <p>Thanks in advance, be kind, it's my first question on SO :-)</p>