[英]Python regex does not match. I cant handle it
why this match does not work ? 为什么这场比赛不起作用? The problem is with D
问题出在D
import re
A = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
B = [0xa8, 0x2c, 0x53, 0x20, 0xca, 0x62, 0x49, 0x13]
C = [0x1A, 0xC4, 0x17, 0x05, 0x47, 0xE8, 0xA3, 0x83]
D = [0x81, 0x63, 0x1f, 0x55, 0xdb, 0x18, 0x2a, 0xab]
for bin_header in [A, B, C, D]:
bin_str = ''.join(map(chr, bin_header))
r = re.match(bin_str, bin_str)
if not r:
print map(hex, bin_header) # Surprise it prints D
You are trying to match a string with a regular expression, but ignoring the regular expression syntax. 您正在尝试将字符串与正则表达式匹配,但忽略了正则表达式语法。 Here is your problem:
这是您的问题:
>>> chr(0x2a)
'*'
The *
has special meaning in regular expression syntax. *
在正则表达式语法中具有特殊含义。 "abc*"
the regular expression won't match "abc*"
the string (it will match "abcccc"
for example). "abc*"
正则表达式与字符串"abc*"
不匹配(例如,它将与"abcccc"
匹配)。
I suggest you use =
or x in y
not re.match
for this. 我建议您为此使用
=
或x in y
不是re.match
。 Passing random bytes to a regular expression isn't a great idea if you don't know that they are all valid characters, or even what the characters are. 如果您不知道它们都是有效字符,甚至不知道这些字符是什么,那么将随机字节传递给正则表达式并不是一个好主意。
Here's an example using in
: 下面是使用的例子
in
:
import re
A = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
B = [0xa8, 0x2c, 0x53, 0x20, 0xca, 0x62, 0x49, 0x13]
C = [0x1A, 0xC4, 0x17, 0x05, 0x47, 0xE8, 0xA3, 0x83]
D = [0x81, 0x63, 0x1f, 0x55, 0xdb, 0x18, 0x2a, 0xab]
for bin_header in [A, B, C, D]:
bin_str = ''.join(map(chr, bin_header))
matches = bin_str in bin_str
if not matches:
print map(hex, bin_header) # Matches all examples.
Even so, constructing strings from unknown byte streams won't play nicely with character encodings, and you should use the correct methods for working with byte sequences. 即使这样,从未知字节流构造字符串也不能很好地与字符编码一起使用,并且您应该使用正确的方法来处理字节序列。
If you really want to use strings for this, you could represent them as hex strings. 如果您确实想为此使用字符串,则可以将它们表示为十六进制字符串。 As hex strings are only
0-9a-z
you could safely use any string or regular expression matching etc. 由于十六进制字符串仅为
0-9a-z
您可以安全地使用任何字符串或正则表达式匹配等。
for bin_header in [A, B, C, D]:
bin_str = ''.join('%02x' % i for i in bin_header)
matches = bin_str in bin_str
print(bin_str, matches)
if not matches:
print map(hex, bin_header)
gives 给
('ffffffffffffffff', True)
('a82c5320ca624913', True)
('1ac4170547e8a383', True)
('81631f55db182aab', True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.