简体   繁体   中英

Regular expression for binary files

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

如果您已经知道文件的二进制结构,那么struct unpack是您的朋友。

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM