A better RE?

Schüle Daniel uval at rz.uni-karlsruhe.de
Thu Mar 9 17:31:43 EST 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?
> 
> P.S. I know the "now you have two problems reply..."

 >>> txt = "21MAR06 31APR06 1236"

 >>> m = '(?:JAN|FEB|MAR|APR|MAI|JUN|JUL|AUG|SEP|OCT|NOV|DEZ)'
# non capturing group (:?)

 >>> p = re.compile(r"(\d\d%s\d\d) (\d\d%s\d\d) 
(?=[1234567])(1?2?3?4?5?6?7?)" % (m,m))

 >>> p.match(txt).group(1)
'21MAR06'

 >>> p.match(txt).group(2)
'31APR06'

 >>> p.match(txt).group(3)
1236




More information about the Python-list mailing list