python: regular expression search pattern for binary files (half a byte)

2024/10/8 0:25:35

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

Answer

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].")
https://en.xdnf.cn/q/70178.html

Related Q&A

pandas: selecting rows in a specific time window

I have a dataset of samples covering multiple days, all with a timestamp. I want to select rows within a specific time window. E.g. all rows that were generated between 1pm and 3 pm every day.This is a…

How to visualize (dendrogram) a dictionary of hierarchical items?

This is my first time of doing visualization from hierarchical data in dictionary format with Python. Last part of the data looks like this:d = {^2820: [^391, ^1024], ^2821: [^759, w, ^118, ^51], ^2822…

How to unfocus (blur) Python-gi GTK+3 window on Linux

What I want to do and whyI want my window to unfocus, so the previous focused window is selected.Why? I want to interact with the previously selected window (from other programs). My current plan is: …

SyntaxError: multiple exception types must be parenthesized

I am a beginner and have a problem after installing pycaw for the audio control using python, on putting the basic initialization code for pycaw, i get the following error:- Traceback (most recent call…

Python find difference between file paths

I have a bunch of file paths, such as:path1 = "./base/folder1/subfolder" path2 = "./base/folder2/"I am trying to write a function that can give me the relative difference between th…

Update range of colorbar in matplotlib

I want to update a contourf plot within a function, which works fine. However, the range of the data changes and I therefore also have to update the colorbar. That is where I fail to do so.Please see f…

Anaconda - arrow keys not work well in python shell

I installed Anaconda3 on manjaro (with i3wm and Urxvt). When I go into python interpreter, it is OK to type python script and execute. But when key arrows are pressed to call up history, everything mes…

couldnt remove origin point in matplotlib polycollection

I have tried an example with PolyCollection from matplotlib tutorials and noticed one strange thing. I couldnt remove this points from axes origin see fig. How do I manage this?from mpl_toolkits.mplot…

How to read .odt using Python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.The com…

Modifying Django settings in tests

From Django docs:You shouldn’t alter settings in your applications at runtime. Forexample, don’t do this in a view:from django.conf import settingssettings.DEBUG = True # Dont do this!The only plac…