JohnnyB
JohnnyB

Reputation: 5

using a regex wildcard within a specific pattern match

my code:

f = open("file.bin", 'rb')
s = f.read()
str1 = ''.join(re.findall( b'\x00\x00\x00\x12\x00\x00\x00(.*?)\x00\x01\x00\x00', s )[0])

I have some binary files from which I want to extract information (strings). The information/strings in this file looks like "[DELIMITER]String1[DELIMITER]STRING2"... The delimiters used in these files are always different but the 00's are always the same so a good workaround would be to tell regex that \x12 and \x01 can be anything.

So what I would need is

str1 = ''.join(re.findall( b'\x00\x00\x00\x[ANYTHING]\x00\x00\x00(.*?)\x00\x[ANYTHING]\x00\x00', s )[0])

How can I do this in regex?

Upvotes: 0

Views: 345

Answers (1)

You could try

str1 = ''.join(re.findall(b'\x00\x00\x00.\x00\x00\x00(.*?)\x00.\x00\x00', s)[0], re.S)

The re.S is needed for . to match absolutely any character (or byte in this case), including \n (aka \x0a).

(Notice that to the regular expression engine, each \xnn is just 1 character, so you cannot use any operators within such escape).

Upvotes: 1

Related Questions