title: replace capturing group
body: <p>If I have a regex with a capturing group, e.g. <code>foo(_+f)</code>. If I match this against a string and want to replace the first capturing group <em>in all matches</em> with <code>baz</code> so that</p>
  
  <pre><code>foo___f blah foo________f
  </code></pre>
  
  <p>is converted to:</p>
  
  <pre><code>foobaz blah foobaz
  </code></pre>
  
  <p>There doesn't appear to be any easy way to do this using the standard libraries. If I use  <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.html#replaceAll(java.lang.String)" rel="noreferrer">Matcher.replaceAll()</a> this will replace all matches of the entire pattern and convert the string to </p>
  
  <pre><code>baz blah baz
  </code></pre>
  
  <p>Obviously I can just iterate through the matches, store the start and end index of each capturing group, then go back and replace them, but is there an easier way?</p>
  
  <p>Thanks,
  Don</p>