Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

RegEx on Binary data

by rdaniels_77 (Initiate)
on Oct 02, 2012 at 19:25 UTC ( #996899=perlquestion: print w/replies, xml ) Need Help??
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 (Prior) 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/;



      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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://996899]
Approved by Perlbotics
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2016-09-01 02:01 GMT
Find Nodes?
    Voting Booth?
    The best thing I ever won in a lottery was:

    Results (437 votes). Check out past polls.