alphanumeric alphanumeric - 1 year ago 89
Python Question

How to use multiple token using Regex Expression

To extract first three letters 'abc' and three sets of three-digits numbers in

I am using the following expression:

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

But the expression returns empty list when instead of underscores there are minuses or periods used instead:
or any combination of it like:

Sure I could use a simple replace method to unify the text variable

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

Answer Source

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

You can use a regex like this:

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

enter image description here

Btw, you can shorten your regex to have something like this:

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

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:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download