title: Regex to replace %variables%
body: <p>I've been yanking clumps of hair out for 30 minutes doing this one...</p>
  
  <p>I have a dictionary, like so:</p>
  
  <pre><code>{'search': 'replace',
   'foo':    'bar'}
  </code></pre>
  
  <p>And a string like this:</p>
  
  <pre><code>Foo bar %foo% % search %.
  </code></pre>
  
  <p>I'd like to replace each variable with it's equivalent text from the dictionary:</p>
  
  <pre><code>Foo bar bar replace.
  </code></pre>
  
  <p>My current regex fails, so here it is (<code>key</code> and <code>value</code> are from <code>dictionary.items()</code>):</p>
  
  <pre><code> re.sub(r'%\d+' + key + '[^%]\d+%', value, text)
  </code></pre>
  
  <p>Any help would be appreciated, as this regex stuff is driving me nuts...</p>