Is possible to use regex to read binary file?

Important Notice


Please note that on 14th December 2023 users will experience an expected outage whilst we make upgrades to our network. We anticipate this process may take a couple of hours and so we apologise in advance for any inconvenience.

  • Re: Is possible to use regex to read binary file?


    Pattern I found is

  • Re: Is possible to use regex to read binary file?


    The goal is print in different rows (one for each block) the values got with regex1 and regex2(if exists).


    Maybe somebody could help in how to apply the regexes for variable that stores each block.


    Thanks in advance

  • Re: Is possible to use regex to read binary file?


    Quote from jindon;685790

    Pattern I found is


    Hello jindon,


    I'll test your code when I get a PC. But is possible to use back-reference?
    Because the need to process the pattern2 that has variable length, so I'd like to store the content of pattern1 and patterne in different variables.


    Thanks for the help.

  • Re: Is possible to use regex to read binary file?


    Simple

  • Re: Is possible to use regex to read binary file?


    Hello jindon,


    Thank you for your help!


    I was able to modify your code to get 5 substrings and works fine:



    Now I need to extract some substrings from var4 (var4 is below), the substrings are highlighted in color:
    var4 content:
    900F0102000000308147526905FFFFFF00910F01020000013A81475269559FFFFF009310010C0000009F8147526905FFFFFF0101960F
    010E000000EB81475269596FFFFF
    970F01010006F69981475269563FFFFF00


    As you can see, there are 5 subtrings within this var4. The regex that works to match each of those subtrings is:

    Code
    (9[\d])(.{4})(.{2})(.{8})([^F]*)F*?(0[0-1])(0[0-1])?



    And I use 7 groups () because I need to process each submatch(x) from this pattern.

    The issue is that even there are 5 subtrings in var4, it seems the code I'm using below is only matching one, because
    For Each loop only happens once.


    I don't know how the regex could match more than one substring in var4.


    Thanks again for any help.

  • Re: Is possible to use regex to read binary file?


    See if this helps

  • Re: Is possible to use regex to read binary file?


  • Re: Is possible to use regex to read binary file?


    Hello jindon and snb,


    Thank you for your help. I've been trying and trying and mixing and adapting your 2 codes and it seems to work nice.


    The only issue it seems to be the performance if I try a relative small file of 20MB. Excel got stuck.


    Do you know if when open the file for read can be done only for a few KB (4KB for example)? This in order to avoid buffer problems.


    Thanks again for the help.

  • Re: Is possible to use regex to read binary file?


    Quote from snb;685869

    You know how to read a freefile line by line ...


    Hello snb,


    Thanks for your help. I've tried filefree, but is the same behaviour, it seems a buffer issue or that a relative small
    size of 20 MB cannot be handle by VBA :(.

  • Re: Is possible to use regex to read binary file?


    Hello pike,


    Is a dump file containing information for products and each block is a record for a product and each block contains characteristics of that product.


    The file is dumped into binary format to have a compressed file with the information and yes, it is created to be read as binary. Is the same format with the same patterns as the file I've uploaded, only is bigger. A file of 2GB can be read without issues by a scripting language like ruby block by block separating each line by ff77, but it seems the problem could be that VBA is reading or storing in buffer the complete file and not storing only each block at a time since got stuck when I use a small file of 20MB.


    Thanks again for the help

  • Re: Is possible to use regex to read binary file?


    Hello apo,


    Is because the ruby solution was not done by me, I don't know ruby or python or VB.net. Only have some experience with Excel VBA and having a solution in excel would be much more portable for the users.


    Best regards

  • Re: Is possible to use regex to read binary file?


    Hello pike,


    I dont have the code which generates the dump. It is an application that generates it. What I've seen is doing with regex is the easiest way to get the information for each block, since the lenght of each block is variable and the values of second patterns are not in the same position within each block.


    The issue I see is VBA gets stuck, the algorithm it seems to be fine.

    Best regards.

  • Re: Is possible to use regex to read binary file?


    Hello pike,


    I dont have the code which generates the dump. It is an application that generates it. What I've seen is doing with regex is the easiest way to get the information for each block, since the lenght of each block is variable and the values of second patterns are not in the same position within each block.


    The issue I see is VBA gets stuck, the algorithm it seems to be fine.

    Best regards.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!