为什么python正则表达式似乎无法匹配超过112个字节?

时间:2013-11-14 23:54:39

标签: python regex

我有一个 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,因为内容无关紧要?!

非常感谢!

1 个答案:

答案 0 :(得分:2)

字节113很可能等于\n,(二进制10,十六进制0)。尝试将re.DOTALL标志添加到正则表达式。

然而,如评论中所述,您可能不需要正则表达式。