I'm searching a way to do some string substitution in python 2.7 usin regex on a binary file.
s is a string I get from reading a binary file. It contains this sequence (hex ) :
' 00 00 03 00 00 01 4A 50 20 43 52 55 4E 43 48 20 32 20 45 51 00 F7 00 F0 '
here is the variable I use for finding the string to sub :
f01 = re.findall( br'\x03\x00\x00\x01(.*?)\xF7\x00\xF0', s)
here is my sub :
f99 = re.sub( br'\x03\x00\x00\x01(.*?)\xF7\x00\xF0', br'\x03\x00\x00\x01\x4B\x4B\x4B\x4B\xF7\x00\xF0', s)
now , while I got no error , my sub doesn't seem to change my string. Am I missing something ?
>>> f01 = re.findall( br'\x03\x00\x00\x01(.*?)\xF7\x00\xF0', s)
>>> print f01[0]
JP CRUNCH 2 EQ
>>> f99 = re.sub( br'\x03\x00\x00\x01(.*?)\xF7\x00\xF0', br'\x03\x00\x00\x01\x4B\x4B\x4B\x4B\xF7\x00\xF0', s)
>>> print f99
MThd
>>> print f99[0]
M
>>> print f01[0]
JP CRUNCH 2 EQ
>>> f01 = re.findall( br'\x03\x00\x00\x01(.*?)\xF7\x00\xF0', s)
>>> print f01[0]
JP CRUNCH 2 EQ
I would like to have my initial string changed to \x03\x00\x00\x01\x4B\x4B\x4B\x4B\xF7\x00\xF0 so I can store it to a file.