title: Replace every occurrence of pattern in a string by an incremental pattern
body: <p>I have many input files like this one:</p>
  
  <pre><code>(((A:1,B:2)100:4,C:5)87:4,D:3);
  </code></pre>
  
  <p>I want to replace <code>100</code> and <code>87</code> by <code>#1</code> and <code>#2</code>. So the idea is that everytime the pattern <code>)*:</code> is matched the <code>*</code> is replaced by an incremental string: <code>#1</code>,<code>#2</code>...</p>
  
  <p>The only thing I've figured out up to now is this perl one-liner: <code>perl -ple 's/\)*\:/\)$n++\:/g' file</code> but it gives the following output:</p>
  
  <p><code>(((A)++:1,B)++:2)100)++:4,C)++:5)87)++:4,D)++:3);</code> </p>
  
  <p>and I can't see where I'm wrong.</p>
  
  <p>Any <code>bash</code> or <code>perl</code> (or other!) script would be much appreciated!</p>
  
  <p>Thanks!</p>