Regular Expression problem

Vlastimil Brom vlastimil.brom at gmail.com
Tue Sep 8 11:45:33 CEST 2009


2009/9/8 找尋自己的一片天 <command.bbs at alexbbs.twbbs.org>:
> I have the following source code
>
> ------------------------
> import re
> d = 'RTCB\r\nsignature:\xf1\x11
> \xde\x10\xfe\x0f\x9c\x10\xf6\xc9_\x10\xf3\xeb<\x10\xf2Zt\x10\xef\xd2\x91\x10\xe6\xe7\xfb\x10\xe5p\x99\x10\xe2\x1e\xdf\x10\xdb\x0e\x9f\x10\xd8p\x06\x10\xce\xb3_\x10\xcc\x8d\xe2\x10\xc8\x00\xa4\x10\xc5\x994\x10\xc2={\x10\xc0\xdf\xda\x10\xbb\x03\xa3\x1
> 0\xb6E\n\x10\xacM\x12\x10\xa5`\xaa\x10\xa0\xaa\x1b\x10\x9bwy\x10\x9a\xc4w\x10\x95\xb6\xde\x10\x93o
> \x10\x89N\xd3\x10\x86\xda=\x00\x00\x00\x00\x00\x00\x00\x00\r\ncef-ip:127.0.0.1\r\nsender-ip:152.100.123.77\r\n\r\n'
> m = re.search('signature:(.*?)\r\n',d)
>
> ---------------------------
>
> as you can see, there is "signature:..." in front of d
>
> but re.search can not find the match object, it return None object...
>
> i don't know why this happened??
>
> (i have test other cases, but i met this string which can't be search for)
>
> could anyone have any suggestions?
>
> --
>  [1;36m※Post by  [37mcommand        [36mfrom  [33m59-124-255-226.HINET-IP. [m
>  [1;36m老鼠的香香乳酪洞 [31m˙ [33m電子佈告欄系統 [31m˙ [32malexbbs.twbbs.org [31m˙ [37m140.113.166.7 [m
> --
> http://mail.python.org/mailman/listinfo/python-list
>

I seems, that the problem is in the . [dot] not matching the newline
character by default; there is a "\n" before the first next "\r\n".

If this is intentional (i.e.the mix of line endings in one string) and
you want to make dot match any character, use e.g. the search pattern:
(?s)signature:(.*?)\r\n

hth,
  vbr



More information about the Python-list mailing list