rdaniels_77 has asked for the wisdom of the Perl Monks concerning the following question:

I've not been successful in finding a lot of info using Perl RegEx for binary data. Think of a high level C structure:

typedef struct { int ID; int otherInt; unsigned char data[10]; } MYSTRUCT;

Let's say I have an array of MYSTRUCTs in a binary file. I want to look for a pattern, say 0xaa, 0xbb, 0xcc in each 'data' field. If I find it, return true.

For this example let's just say I have 1 structure, so the regex should skip 8 bytes (jump over the 2-32bit integers), then look for the 3 byte pattern 0xaa, 0xbb, 0xcc across the 10 bytes of the data field.

Replies are listed 'Best First'.
Re: RegEx on Binary data
by dave_the_m (Monsignor) on Oct 02, 2012 at 19:37 UTC
    Like runrig said, you probably want to unpack the individual fields first, but in answer to your question, perhaps something like:
    $packed_record =~ /^[\x00-\xff]{8,15}\xaa\xbb\xcc/;

    Dave.

      Thanks...

      The library approach doesn't actually work for me. I should have prefaced that I am not using raw Perl, but compiled PCRE in C language, so I needed the raw RegEx.

      I'll play with that expression...

Re: RegEx on Binary data
by runrig (Abbot) on Oct 02, 2012 at 19:31 UTC