Regular Expression problem

  • Thread starter §ä´M¦Û¤vªº¤@¤ù¤Ñ
  • Start date
§

§ä´M¦Û¤vªº¤@¤ù¤Ñ

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?
 
V

Vlastimil Brom

2009/9/8 §ä´M¦Û¤vªº¤@¤ù¤Ñ said:
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?

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
 
S

Steven D'Aprano

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
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...


That's because you're trying to match over multiple lines. You need to
specify the DOTALL flag.


I've re-formatted the string constant to take advantage of Python string
concatenation, so it is easier to copy and paste into the interactive
interpreter:


d =('RTCB\r\nsignature:'
'\xf1\x11\xde\x10\xfe\x0f\x9c\x10\xf6\xc9'
'_\x10\xf3\xeb'
'<\x10\xf2'
'Zt\x10\xef\xd2\x91\x10\xe6\xe7\xfb\x10\xe5'
'p\x99\x10\xe2\x1e\xdf\x10\xdb\x0e\x9f\x10\xd8'
'p\x06\x10\xce\xb3'
'_\x10\xcc\x8d\xe2\x10\xc8\x00\xa4\x10\xc5\x99'
'4\x10\xc2'
'={\x10\xc0\xdf\xda\x10\xbb\x03\xa3\x10\xb6'
'E\n\x10\xac'
'M\x12\x10\xa5'
'`\xaa\x10\xa0\xaa\x1b\x10\x9b'
'wy\x10\x9a\xc4'
'w\x10\x95\xb6\xde\x10\x93'
'o\x10\x89'
'N\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'
)
assert len(d) == 182

<_sre.SRE_Match object at 0xb7e98138>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top