Finding pattern in binary file?
up vote
0
down vote
favorite
I have this two functions:
def make_regex_from_hex_sign(hex_sign):
regex_hex_sign = re.compile(hex_sign.decode('hex'))
return regex_hex_sign
def find_regex_pattern_and_return_its_offset(regex_pattern, bytes_array):
if found_regex_pattern in regex_pattern.finditer(bytes_array):
return found_regex_pattern.start()
else:
return 0
and i'm using them like this:
pattern = make_regex_from_hex_sign("634351535F")
file = open('somefile.bin', 'rb')
allbytes = file.read()
offset = find_regex_pattern_and_return_its_offset(pattern, allbytes)
Python throws: NameError: global name 'found_regex_pattern' is not defined
If i replace if
with for
in if found_regex_pattern in regex_pattern.finditer(bytes_array)
it works, but then i need to break
at the end to stop it from searching past first found pattern iteration. Is there more elegant way to solve this without using for
and break
?
python python-2.x
add a comment |
up vote
0
down vote
favorite
I have this two functions:
def make_regex_from_hex_sign(hex_sign):
regex_hex_sign = re.compile(hex_sign.decode('hex'))
return regex_hex_sign
def find_regex_pattern_and_return_its_offset(regex_pattern, bytes_array):
if found_regex_pattern in regex_pattern.finditer(bytes_array):
return found_regex_pattern.start()
else:
return 0
and i'm using them like this:
pattern = make_regex_from_hex_sign("634351535F")
file = open('somefile.bin', 'rb')
allbytes = file.read()
offset = find_regex_pattern_and_return_its_offset(pattern, allbytes)
Python throws: NameError: global name 'found_regex_pattern' is not defined
If i replace if
with for
in if found_regex_pattern in regex_pattern.finditer(bytes_array)
it works, but then i need to break
at the end to stop it from searching past first found pattern iteration. Is there more elegant way to solve this without using for
and break
?
python python-2.x
This is a scoping issue entirely unrelated to what you're trying to do. Your indentation is wonky so that needs fixing before this can be definitively answered
– roganjosh
Nov 10 at 19:17
Sorry, just noticed it. Fixed.
– alexstx
Nov 10 at 19:18
Using thefor
loop will iterate overregex_pattern.finditer(bytes_array)
with a loop variable calledfound_regex_pattern
. Usingif
will try to resolve findingfound_regex_pattern
withinregex_pattern.finditer(bytes_array)
. As mentioned above, this will fail if there is no variablefound_regex_pattern
in scope. But these are two completely different tasks. You should figure out what you're trying to do and use the right construct for it.
– ahota
Nov 10 at 19:20
Thanks for answers. Actually i want the same behavior as usingfor
, but without it because i only need the first found pattern iteration.
– alexstx
Nov 10 at 19:25
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have this two functions:
def make_regex_from_hex_sign(hex_sign):
regex_hex_sign = re.compile(hex_sign.decode('hex'))
return regex_hex_sign
def find_regex_pattern_and_return_its_offset(regex_pattern, bytes_array):
if found_regex_pattern in regex_pattern.finditer(bytes_array):
return found_regex_pattern.start()
else:
return 0
and i'm using them like this:
pattern = make_regex_from_hex_sign("634351535F")
file = open('somefile.bin', 'rb')
allbytes = file.read()
offset = find_regex_pattern_and_return_its_offset(pattern, allbytes)
Python throws: NameError: global name 'found_regex_pattern' is not defined
If i replace if
with for
in if found_regex_pattern in regex_pattern.finditer(bytes_array)
it works, but then i need to break
at the end to stop it from searching past first found pattern iteration. Is there more elegant way to solve this without using for
and break
?
python python-2.x
I have this two functions:
def make_regex_from_hex_sign(hex_sign):
regex_hex_sign = re.compile(hex_sign.decode('hex'))
return regex_hex_sign
def find_regex_pattern_and_return_its_offset(regex_pattern, bytes_array):
if found_regex_pattern in regex_pattern.finditer(bytes_array):
return found_regex_pattern.start()
else:
return 0
and i'm using them like this:
pattern = make_regex_from_hex_sign("634351535F")
file = open('somefile.bin', 'rb')
allbytes = file.read()
offset = find_regex_pattern_and_return_its_offset(pattern, allbytes)
Python throws: NameError: global name 'found_regex_pattern' is not defined
If i replace if
with for
in if found_regex_pattern in regex_pattern.finditer(bytes_array)
it works, but then i need to break
at the end to stop it from searching past first found pattern iteration. Is there more elegant way to solve this without using for
and break
?
python python-2.x
python python-2.x
edited Nov 10 at 19:17
asked Nov 10 at 19:14
alexstx
385
385
This is a scoping issue entirely unrelated to what you're trying to do. Your indentation is wonky so that needs fixing before this can be definitively answered
– roganjosh
Nov 10 at 19:17
Sorry, just noticed it. Fixed.
– alexstx
Nov 10 at 19:18
Using thefor
loop will iterate overregex_pattern.finditer(bytes_array)
with a loop variable calledfound_regex_pattern
. Usingif
will try to resolve findingfound_regex_pattern
withinregex_pattern.finditer(bytes_array)
. As mentioned above, this will fail if there is no variablefound_regex_pattern
in scope. But these are two completely different tasks. You should figure out what you're trying to do and use the right construct for it.
– ahota
Nov 10 at 19:20
Thanks for answers. Actually i want the same behavior as usingfor
, but without it because i only need the first found pattern iteration.
– alexstx
Nov 10 at 19:25
add a comment |
This is a scoping issue entirely unrelated to what you're trying to do. Your indentation is wonky so that needs fixing before this can be definitively answered
– roganjosh
Nov 10 at 19:17
Sorry, just noticed it. Fixed.
– alexstx
Nov 10 at 19:18
Using thefor
loop will iterate overregex_pattern.finditer(bytes_array)
with a loop variable calledfound_regex_pattern
. Usingif
will try to resolve findingfound_regex_pattern
withinregex_pattern.finditer(bytes_array)
. As mentioned above, this will fail if there is no variablefound_regex_pattern
in scope. But these are two completely different tasks. You should figure out what you're trying to do and use the right construct for it.
– ahota
Nov 10 at 19:20
Thanks for answers. Actually i want the same behavior as usingfor
, but without it because i only need the first found pattern iteration.
– alexstx
Nov 10 at 19:25
This is a scoping issue entirely unrelated to what you're trying to do. Your indentation is wonky so that needs fixing before this can be definitively answered
– roganjosh
Nov 10 at 19:17
This is a scoping issue entirely unrelated to what you're trying to do. Your indentation is wonky so that needs fixing before this can be definitively answered
– roganjosh
Nov 10 at 19:17
Sorry, just noticed it. Fixed.
– alexstx
Nov 10 at 19:18
Sorry, just noticed it. Fixed.
– alexstx
Nov 10 at 19:18
Using the
for
loop will iterate over regex_pattern.finditer(bytes_array)
with a loop variable called found_regex_pattern
. Using if
will try to resolve finding found_regex_pattern
within regex_pattern.finditer(bytes_array)
. As mentioned above, this will fail if there is no variable found_regex_pattern
in scope. But these are two completely different tasks. You should figure out what you're trying to do and use the right construct for it.– ahota
Nov 10 at 19:20
Using the
for
loop will iterate over regex_pattern.finditer(bytes_array)
with a loop variable called found_regex_pattern
. Using if
will try to resolve finding found_regex_pattern
within regex_pattern.finditer(bytes_array)
. As mentioned above, this will fail if there is no variable found_regex_pattern
in scope. But these are two completely different tasks. You should figure out what you're trying to do and use the right construct for it.– ahota
Nov 10 at 19:20
Thanks for answers. Actually i want the same behavior as using
for
, but without it because i only need the first found pattern iteration.– alexstx
Nov 10 at 19:25
Thanks for answers. Actually i want the same behavior as using
for
, but without it because i only need the first found pattern iteration.– alexstx
Nov 10 at 19:25
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You did not define found_regex_pattern.
When you do the change from if to for it works because its a valid syntax and that means that found_regex_pattern acts as an entry of the regex_pattern.finditer(bytes_array) iterable.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You did not define found_regex_pattern.
When you do the change from if to for it works because its a valid syntax and that means that found_regex_pattern acts as an entry of the regex_pattern.finditer(bytes_array) iterable.
add a comment |
up vote
0
down vote
You did not define found_regex_pattern.
When you do the change from if to for it works because its a valid syntax and that means that found_regex_pattern acts as an entry of the regex_pattern.finditer(bytes_array) iterable.
add a comment |
up vote
0
down vote
up vote
0
down vote
You did not define found_regex_pattern.
When you do the change from if to for it works because its a valid syntax and that means that found_regex_pattern acts as an entry of the regex_pattern.finditer(bytes_array) iterable.
You did not define found_regex_pattern.
When you do the change from if to for it works because its a valid syntax and that means that found_regex_pattern acts as an entry of the regex_pattern.finditer(bytes_array) iterable.
answered Nov 10 at 19:18
Juan Ignacio Sánchez
305111
305111
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242522%2ffinding-pattern-in-binary-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This is a scoping issue entirely unrelated to what you're trying to do. Your indentation is wonky so that needs fixing before this can be definitively answered
– roganjosh
Nov 10 at 19:17
Sorry, just noticed it. Fixed.
– alexstx
Nov 10 at 19:18
Using the
for
loop will iterate overregex_pattern.finditer(bytes_array)
with a loop variable calledfound_regex_pattern
. Usingif
will try to resolve findingfound_regex_pattern
withinregex_pattern.finditer(bytes_array)
. As mentioned above, this will fail if there is no variablefound_regex_pattern
in scope. But these are two completely different tasks. You should figure out what you're trying to do and use the right construct for it.– ahota
Nov 10 at 19:20
Thanks for answers. Actually i want the same behavior as using
for
, but without it because i only need the first found pattern iteration.– alexstx
Nov 10 at 19:25