简体   繁体   English

二进制文件的正则表达式

[英]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 2字节,2字节,18字节

如果您已经知道文件的二进制结构,那么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? 您是说要连续解析2-2-18个字节块中的字节吗? In that case, why not just read that many bytes as you go along using standard IO methods? 在这种情况下,为什么不使用标准IO方法读取那么多字节呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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