title: Ant, replaceregexp, match and replace specific element in the list of matches
body: <p>In my ant build file, I have a task that needs to replace a specific element of a XML. </p>
  
  <p>Here is the target XML that I am trying to modify:</p>
  
  <pre><code>&lt;foo&gt;
   &lt;sub&gt;
    &lt;elem&gt;name1&lt;/elem&gt;
   &lt;/sub&gt;
   &lt;sub&gt;
    &lt;elem&gt;name2&lt;/elem&gt;
   &lt;/sub&gt;
   &lt;sub&gt;
    &lt;elem&gt;name3&lt;/elem&gt;
   &lt;/sub&gt;
  &lt;/foo&gt;
  </code></pre>
  
  <p>Ant build task:</p>
  
  <pre><code>&lt;replaceregexp file="myfoo.xml"
        match="&amp;lt;elem&amp;gt;(.*)elem&amp;gt;"
        replace="&amp;lt;elem&amp;gt;${replace_only_second_match}elem&amp;gt;"
        byline="true"
  /&gt;
  </code></pre>
  
  <p>The problem with the above task is that all the  tags will get replaced. However, I want only the second element to be modified, not the first or 3rd match. (such a thing is quite easy with normal regular expressions.)</p>
  
  <p>Dont know how to do it with Ant's regular expression. This is where I need help/suggestions on how best to solve this problem.</p>