我有一个文件what.dmp,它有 116 个字节长。我的 python 代码如下所示:
import binascii
import re
import sys
print(sys.version)
needle = re.compile(b".{112}")
with open("what.dmp", "rb") as haystack:
chunk = haystack.read()
print("Read {0} bytes.".format(len(chunk)))
matches = needle.search(chunk)
if matches:
print(matches.start())
print(binascii.hexlify(matches.group(0)))
else:
print("No matches found.")
运行此代码很好:
C:\test>C:\Python33\python.exe test.py
3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)]
Read 116 bytes.
0
b'0101060001010600087e88758f4e8e75534589751df7897583548775e4bcf001e6d0f001cae3f001ccf7f0010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000090d91300000000002c003100eb6fb024'
但是,将正则表达式从 112 更改为 113:
needle = re.compile(b".{113}")
并且没有找到匹配项:
C:\test>C:\Python33\python.exe test.py
3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)]
Read 116 bytes.
No matches found.
所以问题是:为什么正则表达式不匹配第 113 个字符。我没有发布what.dmp,因为内容肯定是无关紧要的?!
非常感谢!