Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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.

Comment on RegEx on Binary data
Download Code
Replies are listed 'Best First'.
Re: RegEx on Binary data
by dave_the_m (Parson) 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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2015-07-08 07:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (96 votes), past polls