Python Regular Expression; replacing a portion of match
<p>How would I limit match/replacement the leading zeros in e004_n07?  However, if either term contains all zeros, then I need to retain one zero in the term (see example below). For the input string, there will always be 3 digits in the first value and 2 digits in the second value.</p>

<p>Example input and output</p>

<pre><code>e004_n07 #e4_n7
e020_n50 #e20_n50
e000_n00 #e0_n0
</code></pre>

<p>Can this be accomplished with re.sub alone, or do I need to use re.search/re.match?</p>

<p>There's no need to use <code>re.sub</code> if your replacement is so simple - simply use <a class='doc-link' href="http://stackoverflow.com/documentation/python/278/string-methods/1008/replace-all-occurrences-of-one-substring-with-another-substring#t=201608291922416212581"><code>str.replace</code></a>:</p>

<pre><code>s = 'e004_n07'
s.replace('0', '') # =&gt; 'e4_n7'
</code></pre>

python regex