I am using the following regular expression pattern for searching 0xDEAD4FAD in a binary file:
my_pattern = re.compile(b"\xDE\xAD\x4F\xAD")
but how do I generalize the search pattern for searching 0xDEAD4xxx? can't seem to cut through half a byte
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
To answer your specific question about searching for 0xDEAD4xxx, use:
my_pattern = re.compile(b"\xDE\xAD[\x40-\x4F].")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With