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?










share|improve this question
























  • 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 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

















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?










share|improve this question
























  • 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 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















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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




















  • 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 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


















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














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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 19:18









        Juan Ignacio Sánchez

        305111




        305111






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Full-time equivalent

            Bicuculline

            さくらももこ