title: Is there a way to replace regexes dynamically in Python?
body: <p>Some programming languages offer the ability to perform a regex replacement dynamically.</p>
  
  <p>For instance, say we have a string like <code>foo:$USER:$GROUP</code>, where <code>$USER</code> and <code>$GROUP</code> will be replaced by their environment variables. The transformed string would look something like <code>foo:john:admin</code>. To solve this problem, we have to take all the strings matching <code>\$[A-Za-z]+</code> and look up the environment variable value.</p>
  
  <p>In PHP, the following looks like this:</p>
  
  <pre><code>&lt;?php
  preg_replace_callback(
     # the regular expression to match the shell variables.
     '/\$[A-Za-z]+/',
     # Function that takes in the matched string and returns the environment
     # variable value.
     function($m) {
       return getenv(substr($m[0], 1));
    },
    # The input string.
    'foo:$USER:$GROUP'
  );
  </code></pre>
  
  <p>Is there a similar thing in Python?</p>