python查找子字符串出现的所有位置

简介

通过正则表达式将字符串中找出所有字符串出现的位置,可以用于文件格式提取等工作…

例子

通用模型

import re
addr = [substr.start() for substr in re.finditer(子字符串, 原字符串)]

比如在原字符串buf中寻找子字符串sub 1出现过的位置

>>> import re
>>> buf = "1231234567890absd11aasda1;;;\x00\x44"
>>> sub = "1"
>>> [substr.start() for substr in re.finditer(sub, buf)]
[0, 3, 17, 18, 24]

查找文件头

在文件中提取ELF文件信息等:

import re
file_type = {
    "\x00": "none",
    "\x01": "rel",
    "\x02": "elf",
    "\x03": "so",
    "\x04": "core"
}

filename = "./test"
buf = open(filename, "rb").read()
sub = "\x7fELF....\x00\x00\x00\x00"
addr = [substr.start() for substr in re.finditer(sub, buf)]
addr.append(len(buf))
for i in range(len(addr) - 1):
    if addr[i + 1] - addr[i] > 0x40:
        data = buf[addr[i]:addr[i + 1]]
        try:
            ftype= file_type[data[0x10]]
            filename = "{}.elf".format(ftype)
            open(filename , "wb").write(data)
        except Exception as e:
            continue

你可能感兴趣的:(Other)