Python正则表达式从二进制文件中提取数据块
我有一个二进制文件。从该文件中,我需要使用python正则表达式提取一些数据块 我需要提取空字符集之间存在的非空字符集 例如,这是主要字符集: \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xfe\xfe\x00\x00\x23\x41\x00\x00\x00\x00\x00\x00\x00\x00\x41\x49\x57\x00\x00\x00\x00\x00\x02\x41\x49\x57\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 正则表达式应从上面的主集合中提取以下字符集: \xff\xfe\xfe\x00\x00\x23\x41, \x41\x49\x57\x00\x00\x00\x00\x32\x41\x49\x57\x00\x00\x00\x32和 \x56\x65\x00\x35\x56 有一点很重要,如果它连续获得5个以上的空字节,那么只有它才应该将这些空字符集视为分隔符。否则,它应该将这些空字节包含到无空字符中。正如您在给定的示例中所看到的,在提取的字符集中也存在一些空字符 如果它没有任何意义,请让我知道,我会尝试以更好的方式解释它Python正则表达式从二进制文件中提取数据块,python,regex,Python,Regex,我有一个二进制文件。从该文件中,我需要使用python正则表达式提取一些数据块 我需要提取空字符集之间存在的非空字符集 例如,这是主要字符集: \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xfe\xfe\x00\x00\x23\x41\x00\x00\x00\x00\x00\x00\x00\x00\x41\x49\x57\x00\x00\x00\x00\x00\x02\x41\x49\x57\x00\x00\x00\x00\x00\x00\x00\x
提前感谢,您可以使用split和lstrip进行列表理解,如下所示:
s='\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xfe\xfe\x00\x00\x23\x41\x00\x00\x00\x00\x00\x00\x00\x00\x41\x49\x57\x00\x00\x00\x00\x32\x41\x49\x57\x00\x00\x00\x00\x32\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x56\x65\x00\x35\x56'
sp=s.split('\x00\x00\x00\x00\x00')
print [i.lstrip('\x00\\') for i in sp if i != ""]
输出:
['\xff\xfe\xfe\x00\x00#A', 'AIW\x00\x00\x00\x002AIW\x00\x00\x00\x002', 'Ve\x005V']
您可以在
\x00{5,}
这是5个或更多的零。这是你指定的计价器 在Perl中,是这样的 Perl测试用例
$strLangs = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xfe\xfe\x00\x00\x23\x41\x00\x00\x00\x00\x00\x00\x00\x00\x41\x49\x57\x00\x00\x00\x00\x32\x41\x49\x57\x00\x00\x00\x00\x32\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x56\x65\x00\x35\x56";
# Remove leading zero's (5 or more)
$strLangs =~ s/^\x00{5,}//;
# Split on 5 or more 0's
@Alllangs = split /\x00{5,}/, $strLangs;
# Print each language characters
foreach $lang (@Alllangs)
{
print "<";
for ( split //, $lang ) {
printf( "%x,", ord($_));
}
print ">\n";
}
《代码><代码><<代码><<代码>>部分部分之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以之所以x56”;
#删除前导零位(5个或更多)
$strLangs=~s/^\x00{5,}/;
#在5个或更多0上拆分
@Alllangs=split/\x00{5,}/,$strLangs;
#打印每种语言的字符
foreach$lang(@Alllangs)
{
打印“\n”;
}
输出>>
<ff,fe,fe,0,0,23,41,>
<41,49,57,0,0,0,0,32,41,49,57,0,0,0,0,32,>
<56,65,0,35,56,>
以下是如何在Python中执行此操作。我必须关闭
str.strip()
并使用前导和尾随空来获取正则表达式模式,以防止在从re.split()返回的结果列表的开头包含额外的空字符串
输出:
['\xff\xfe\xfe\x00\x00#A', 'AIW\x00\x00\x00\x002AIW\x00\x00\x00\x002', 'Ve\x005V']
\xff\xfe\xfe\x00\x00\x23\x41,
\x41\x49\x57\x00\x00\x00\x00\x32\x41\x49\x57\x00\x00\x00\x32,
\x56\x65\x00\x35\x56
你确定要使用正则表达式吗?为什么不直接在\000{5,}
上拆分?@msvalkon任何其他更好/更有效的选项???@sln这里分隔符的长度不是固定的..分隔符应该是\x00*n..我们知道n>=5…@sln你是这个意思吗?arr=re.split(r'\000{5,}',data)@Raza:在你的问题中,你说“连续超过5个空字节”,所以你可能想要re.split(r'\000{6,}',data)
。另外,我在Python的re
模块使用此模式时,在开始时得到了一个额外的零长度项。感谢您的回复!