Regexing help in python
<p>I am wondering if there is an efficient way to check if a number is in the format 1_2_3_4_5_6_7_8_9_0 , where the '_' is a number. 
For Example,</p>

<p><strong>1</strong>9<strong>2</strong>9<strong>3</strong>7<strong>4</strong>2<strong>5</strong>4<strong>6</strong>2<strong>7</strong>4<strong>8</strong>8<strong>9</strong>0<strong>0</strong></p>

<p>My initial though was string indexing, but I quickly discovered that it was too slow. Is there a better way using regexing in python? </p>

<p>Thanks in advance, 
                Arvind</p>

<p>Let's define a string to test:</p>

<pre><code>&gt;&gt;&gt; q = '1929374254627488900'
</code></pre>

<p>Now, let's test it:</p>

<pre><code>&gt;&gt;&gt; q[::2] == '1234567890'
True
</code></pre>

<p>That matches.  Now, let's try one that doesn't match:</p>

<pre><code>&gt;&gt;&gt; q = '1929374254627488901'
&gt;&gt;&gt; q[::2] == '1234567890'
False
</code></pre>

<p>This works because <code>q[::2]</code> returns every other character in the string and those are the characters that you are interested in testing.</p>

<h3>Additional tests</h3>

<p>Suppose we are unsure if the string is all numbers.  In that case, we add a test.  This should pass:</p>

<pre><code>&gt;&gt;&gt; q = '1929374254627488900'
&gt;&gt;&gt; q[::2] == '1234567890' and q.isdigit()
True
</code></pre>

<p>While, because of the <code>a</code>, this should fail:</p>

<pre><code>&gt;&gt;&gt; q = '19293742546274889a0'
&gt;&gt;&gt; q[::2] == '1234567890' and q.isdigit()
False
</code></pre>

<p>We can also test for correct length:</p>

<pre><code>&gt;&gt;&gt; q = '1929374254627488900'
&gt;&gt;&gt; q[::2] == '1234567890' and q.isdigit() and len(q) == 19
True
</code></pre>

python regex string