Problems with regular expressions

Gabriel Genellina gagsl-py2 at yahoo.com.ar
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 medianet.es> escribió:

> I have the next piece of code:
> ------------------------------------------------------------------------
>  if re.search('^(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:

py> re.search('^(taskid|bugid):\\d+',"taskid:1234")
<_sre.SRE_Match object at 0x00ACB1E0>
py> re.search('^(taskid|bugid):\\d+',"asdfa fa taskid:1234")
py> re.search('^(taskid|bugid):\\d+'," taskid:1234")
py> re.search('^(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