How to use multiple token using Regex Expression
<p>To extract first three letters 'abc' and three sets of three-digits numbers in <code>000_111_222</code> I am using the following expression:</p>

<pre><code>text = 'abc_000_111_222'
print re.findall('^[a-z]{3}_[0-9]{3}_[0-9]{3}_[0-9]{3}', text)
</code></pre>

<p>But the expression returns empty list when instead of underscores there are minuses or periods used instead: <code>abc.000.111.222</code> or <code>abc-000-111-222</code> or any combination of it like: <code>abc_000.111-222</code></p>

<p>Sure I could use a simple replace method to unify the text variable <code>text=text.replace('-','_').replace('.','_')</code></p>

<p>But I wonder if instead of replacing I could modify regex expression that would recognize the underscores, minuses and periods.</p>

<p>You can use regex character classes with <code>[</code>...<code>]</code>. For your case, it can be <code>[_.-]</code> (note the hyphen at the end, if it isn't at the end, it will be considered as a range like <code>[a-z]</code>).</p>

<p>You can use a regex like this:</p>

<pre><code>print re.findall('^[a-z]{3}[_.-][0-9]{3}[_.-][0-9]{3}[_.-][0-9]{3}', text)
</code></pre>

<p><a href="https://i.stack.imgur.com/9oYZz.png" rel="nofollow"><img src="https://i.stack.imgur.com/9oYZz.png" alt="enter image description here"></a></p>

<p>Btw, you can shorten your regex to have something like this:</p>

<pre><code>print re.findall('^[a-z]{3}[_.-](\d{3}[_.-]){2}\d{3}', text)
</code></pre>

<p>Just as a comment, in case you want to match the same separator, then you can use capture groups and reference its content like this:</p>

<pre><code>^[a-z]{3}([_.-])[0-9]{3}\1[0-9]{3}\1[0-9]{3}
</code></pre>

python regex