Check for very specified numbers padding
<p>I am trying to check for a list of items in my scene to see if they bear 3 (version) paddings at the end of their name - eg. <code>test_model_001</code> and if they do, that item will be pass and items that do not pass the condition will be affected by a certain function..</p>

<p>Suppose if my list of items is as follows:</p>

<ul>
<li>test_model_01</li>
<li>test_romeo_005</li>
<li>test_charlie_rig</li>
</ul>

<p>I tried and used the following code:</p>

<pre><code>eg_list = ['test_model_01', 'test_romeo_005', 'test_charlie_rig']
for item in eg_list:
    mo = re.sub('.*?([0-9]*)$',r'\1', item)
    print mo
</code></pre>

<p>And it return me <code>01</code> and <code>005</code> as the output, in which I am hoping it will return me just the <code>005</code> only.. How do I ask it to check if it contains 3 paddings? Also, is it possible to include underscore in the check? Is that the best way?</p>

<pre><code>for item in eg_list:
    if re.match(".*_\d{3}$", item):
        print item.split('_')[-1]
</code></pre>

<p>This matches anything which ends in:
<code>_</code> and underscore, <code>\d</code> a digit, <code>{3}</code> three of them, and <code>$</code> the end of the line.</p>

<p><img src="https://www.debuggex.com/i/J09VgczoxIm8zauF.png" alt="Regular expression visualization"></p>

<p><a href="https://www.debuggex.com/r/J09VgczoxIm8zauF" rel="nofollow">Debuggex Demo</a></p>

<p>printing the item, we split it on <code>_</code> underscores and take the last value, index <code>[-1]</code></p>

<hr>

<p>The reason <code>.*?([0-9]*)$</code> doesn't work is because <code>[0-9]*</code> matches 0 or more times, so it can match nothing. This means it will also match <code>.*?$</code>, which will match any string.</p>

<p>See the example on <a href="https://regex101.com/r/lS1rG6/1" rel="nofollow">regex101.com</a></p>

python maya