0

How would you use regular expressions to parse through binary files?

What I have so far

import re

format = re.compile() <--

f.open("input.dat", "rb")

for line in f.readlines():
    data = re.search(format, line)

I'm not sure what to put inside compile as I am only used to writing regex for strings.

specifically I want so break down the format to be:

2 bytes, 2 bytes, 18 bytes

Liondancer
  • 15,721
  • 51
  • 149
  • 255
  • 1
    regexp is the wrong instrument for this. please consider Aif's solution with `unpack` – Pavel May 11 '14 at 23:39

2 Answers2

3

If you know already the binary structure of the file, then struct unpack is your friend.

Aif
  • 11,015
  • 1
  • 30
  • 44
0

I'm not sure I understand why you want to parse a binary file with a regex.

Are you saying you want to continually parse the bytes in 2-2-18 byte chunks? In that case, why not just read that many bytes as you go along using standard IO methods?

khampson
  • 14,700
  • 4
  • 41
  • 43