Help me with this!!!
Steven D'Aprano
steve at REMOVEME.cybersource.com.au
Thu Feb 1 21:17:49 EST 2007
On Thu, 01 Feb 2007 08:53:55 -0800, Ravi Teja wrote:
>> > > It search a text inside that hex value.
>> > > It works perfecly on a txt file but if I open a binary file (.exe,.bin
>> > > ecc...) with the same value it wont work, why?
>> > > Please help!
>>
>> > Because the pattern isn't in the file, perhaps.
>>
>> This pattern IS in the file (I made it and I double check with an hex
>> editor).
>> It display the file correcltly (print line) but...
>
> No! Peter is right. Regular expressions match ASCII representation of
> data, not hex. In simple terms, do you see your pattern when you open
> the file in notepad (or other text editor)? You do not use regex to
> search binary files.
I don't see why not.
>>> pattern = "NULL\0"
>>> source = "\0\01\02-more-bytes-here-NULL\0-more-bytes"
>>> m = re.search(pattern, source)
>>> m.group()
'NULL\x00'
>>> m.span()
(20, 25)
Here's the Original Poster's code again:
regex = re.compile(r"(?si)(\x8B\xF0\x85\xF6)(?P<contents>.*)
(\xC6\x44\x24)",re.IGNORECASE)
file = open(fileName, "rb")
for line in file:
if (match):
print line
file.close()
I suggest that the reason it doesn't work is because he never actually
uses the regex. Presumably the name "match" was assigned somewhere else to
a false value, and so the code simply walks through the file doing nothing.
--
Steven D'Aprano
More information about the Python-list
mailing list