title: Python replace, using patterns in array
body: <p>I need to replace some things in a string using an array, they can look like this:</p>
  
  <pre><code>array = [3, "$x" , "$y", "$hi_buddy"]
  #the first number is number of things in array
  string = "$xena is here $x and $y."
  </code></pre>
  
  <p>I've got another array with things to replace those things, let's say its called rep_array.</p>
  
  <pre><code>rep_array = [3, "A", "B", "C"]
  </code></pre>
  
  <p>For the replacement I use this:</p>
  
  <pre><code>for x in range (1, array[0] + 1):
    string = string.replace(array[x], rep_array[x])
  </code></pre>
  
  <p>But the result is:</p>
  
  <pre><code>string = "Aena is here A and B."
  </code></pre>
  
  <p>But I need to much only lonely $x not $x in another word.
  Result should look like this:</p>
  
  <pre><code>string = "$xena is here A and B."
  </code></pre>
  
  <p>Note that:</p>
  
  <ul>
  <li>all patterns in <code>array</code> start with <code>$</code>.</li>
  <li>a pattern matches if it matches the whole word after <code>$</code>; <code>$xena</code> doesn't match <code>$x</code>, but <code>foo$x</code> <em>would</em> match.</li>
  <li><code>$</code> can be escaped with <code>@</code> and than it should not be matched (for example <code>$x</code> does not match <code>@$x</code>)</li>
  </ul>