Problems with regular expressions
Jay Loden
python at jayloden.com
Thu Jun 14 21:15:15 EDT 2007
Carlos Luis Pérez Alonso wrote:
> 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.
>
> ¿Does anybody has an idea of what is happening here?
>
> Thanks
I'm pretty sure it's the escaping on your regular expression. You should use the raw string operator:
-----
#!/usr/bin/python
import re,sys
def checkLogMsg(logMessage):
if re.search(r'^(taskid|bugid):\d+', logMessage):
return 0
else:
sys.stderr.write("El comentario tiene que contener el taskid:#### o el bugid:####")
return 1
print checkLogMsg('taskid:234')
print checkLogMsg('doesnotmatch')
-----
Outputs:
$ python test_reg.py
0
El comentario tiene que contener el taskid:#### o el bugid:####1
HTH,
-Jay
More information about the Python-list
mailing list