A better RE?

bruno at modulix onurb at xiludom.gro
Fri Mar 10 10:12:14 CET 2006


Magnus Lycka wrote:
> I want an re that matches strings like "21MAR06 31APR06 1236",
> where the last part is day numbers (1-7), i.e it can contain
> the numbers 1-7, in order, only one of each, and at least one
> digit. I want it as three groups. I was thinking of
> 
> r"(\d\d[A-Z]\d\d) (\d\d[A-Z]\d\d) (1?2?3?4?5?6?7?)"
> 
> but that will match even if the third group is empty,
> right? Does anyone have good and not overly complex RE for
> this?

Simplest:

>>> exp = r"(\d{2}[A-Z]{3}\d{2}) (\d{2}[A-Z]{3}\d{2}) (\d+)"
>>> re.match(exp, s).groups()
('21MAR06', '31APR06', '1236')

but this could give you false positive, depending on the real data.

If you want to be as strict as possible, this becomes a little bit hairy.

> P.S. I know the "now you have two problems reply..."

!-)

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list