title: Is there a way to use regular expressions in the replacement string in re.sub() in Python?
body: <p>In Python in the <code>re</code> module there is the following function:</p>
  
  <blockquote>
    <p>re.sub(pattern, repl, string, count=0, flags=0) – Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl. If the pattern isn’t found, string is returned unchanged.</p>
  </blockquote>
  
  <p>I've found it can work like this:</p>
  
  <pre><code>print re.sub('[a-z]*\d+','lion','zebra432') # prints 'lion'
  </code></pre>
  
  <p>I was wondering, is there an easy way to use regular expressions in the replacement string, so that the replacement string contains part of the original regular expression/original string? Specifically, can I do something like this (which doesn't work)?</p>
  
  <pre><code>print re.sub('[a-z]*\d+', 'lion\d+', 'zebra432')
  </code></pre>
  
  <p>I want that to print <code>'lion432'</code>. Obviously, it does not. Rather, it prints <code>'lion\d+'</code>. Is there an easy way to use parts of the matching regular expression in the replacement string?</p>
  
  <p>By the way, this is NOT a special case. Please do NOT assume that the number will always come at the end, the words will always come in the beginning, etc. I want to know a solution to all regexes in general. </p>
  
  <p>Thanks</p>