title: regex: how to replace all occurrences of a string within another string, if the original string matches some filter
body: <ul>
  <li>i need to replace all occurrences of a string within another string, if the original string matches some filter</li>
  <li>i can only use a single regex using an <code>s</code> command, because i need to send the assembled command to a 3rd party API</li>
  </ul>
  
  <p>i have tried to use positive lookahead as to not consume the string in which i want to replace characters, but somehow i can not get the replacing to work as expected.</p>
  
  <p>here is what i have tried so far and what was the outcome:
  (note that the filter - here <code>[0-9]+</code> is just an example and will be passed in from the call site and i can not directly influence it. </p>
  
  <p><strong>expected result:</strong> <code>9999997890</code></p>
  
  <p><code>perl -e '$x = "4564567890"; $x =~ s/(?=^[0-9]+$)456/999/g; print $x'</code></p>
  
  <p><strong>actual result:</strong> <code>9994567890</code></p>
  
  <ol>
  <li>this replaces only the first occurrence of <code>456</code>. why is this happening?</li>
  <li>even less understandable for me is that if i change the filter lookahead to <code>(?=.*)</code>, both occurrences of 456 are being replaced. why does changing the filter have any effect on the replacing portion of the regex?</li>
  </ol>
  
  <p>i seem to be missing some very basic point about how mixing filtering and replacing stuff in one <code>s</code> command works.</p>