Problems with regular expressions

Gabriel Genellina gagsl-py2 at
Fri Jun 15 03:30:38 CEST 2007

En Thu, 14 Jun 2007 21:42:02 -0300, Carlos Luis Pérez Alonso  
<CarlosLuis.Perez at> escribió:

> I have the next piece of code:
> ------------------------------------------------------------------------
>  if'^(taskid|bugid):\\d+',logMessage):
>         return 0
> else:
>         sys.stderr.write("El comentario tiene que contener el  
> taskid:#### o el bugid:####")
>         return 1
> -------------------------------------------------------------------------
> The regular exprexión is "^(taskid|bugid):\\d+"
> Mi problem is that if logMessage == "taskid:234" the regular expression  
> matched teorically, but the result is always "None" and the function  
> returns 1.

For "taskid:234" it matches ok:

<_sre.SRE_Match object at 0x00ACB1E0>
py>'^(taskid|bugid):\\d+',"asdfa fa taskid:1234")
py>'^(taskid|bugid):\\d+'," taskid:1234")
py>'^(taskid|bugid):\\d+',"taskid:123adfa asfa lkjljlj")
<_sre.SRE_Match object at 0x00ACB8A0>

Because of the ^, it *only* matches at the start of the line. If you want  
a match anywhere (as implied by your error message), just remove the ^.

Gabriel Genellina

More information about the Python-list mailing list