title: Why is "$1" ending up in my Regex.Replace() result?
body: <p>I am trying to write a regular expression to rewrite URLs to point to a proxy server.</p>
  
  <pre><code>bodystring = Regex.Replace(bodystring, "(src='/+)", "$1" + proxyStr);
  </code></pre>
  
  <p>The idea of this expression is pretty simple, basically find instances of "src='/" or "src='//" and insert a PROXY url at that point. This works in general but occasionally I have found cases where a literal "$1" will end up in the result string.</p>
  
  <p>This makes no sense to me because if there was no match, then why would it replace anything at all?</p>
  
  <p>Unfortunately I can't give a simple example of this at it only happens with very large strings so far, but I'd like to know conceptually what could make this sort of thing happen.</p>
  
  <p>As an aside, I tried rewriting this expression using a positive lookbehind as follows:</p>
  
  <pre><code>bodystring = Regex.Replace(bodystring, "(?&lt;=src='/+)", proxyStr);
  </code></pre>
  
  <p>But this ends up with proxyStr TWICE in the output if the input string contains "src='//". This also doesn't make much sense to me because I thought that "src=" would have to be present in the input twice in order to get proxyStr to end up twice in the output.</p>