简体   繁体   中英

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?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM