python:二进制文件的正则表达式搜索模式(半字节) [英] python: regular expression search pattern for binary files (half a byte)
本文介绍了python:二进制文件的正则表达式搜索模式(半字节)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下正则表达式模式在二进制文件中搜索0xDEAD4FAD:
I am using the following regular expression pattern for searching 0xDEAD4FAD in a binary file:
my_pattern = re.compile(b"\xDE\xAD\x4F\xAD")
但是如何概括搜索0xDEAD4xxx的搜索模式?似乎无法切入半个字节
but how do I generalize the search pattern for searching 0xDEAD4xxx? can't seem to cut through half a byte
推荐答案
正则表达式确实允许在范围内进行搜索.因此,要找到第一个半字节为"4"的字节,请使用:
Regular expressions do allow searching over ranges. Thus, to find a byte whose the first nibble is "4" use:
pattern = re.compile(b"[\x40-\x4F]")
以下测试表明它会产生所需的输出:
The following test shows that it produces the desired output:
>>> for byte in ('\x3f', '\x40', '\x42', '\x4f', '\x50'): print bool(pattern.search(byte))
...
False
True
True
True
False
要回答有关搜索0xDEAD4xxx的特定问题,请使用:
To answer your specific question about searching for 0xDEAD4xxx, use:
my_pattern = re.compile(b"\xDE\xAD[\x40-\x4F].")
这篇关于python:二进制文件的正则表达式搜索模式(半字节)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文