【发布时间】:2015-04-02 23:14:00
【问题描述】:
我正在尝试编写一个脚本来解析由编译器/链接器生成的映射文件,如下所示:
%SEGMENT_SECTION
Start Address End Address
--------------------------------------------------------------------------------
Segment Name: S1_1, Segment Type: .bss 0A000000 0A050F23
--------------------------------------------------------------------------------
area1_start.o (.bss) 0A000000 0A000003
...
Start Address End Address
--------------------------------------------------------------------------------
Segment Name: S2_1, Segment Type: .bss 0A050F24 0A060000
--------------------------------------------------------------------------------
area2_start.o (.bss) 0A000000 0A000003
...
%NEXT_SECTION
我目前正在编写几个正则表达式(python 的 re 模块)来解析它,但我想以一种非常易于阅读的方式编写它们,这样解析起来非常简单。本质上:
with open('blah.map') as f:
text = f.read()
# ... Parse the file to update text to be after the %SEGMENT_SECTION
match = segment_header_re.match(text)
seg_name, seg_type, start_addr, end_addr = match.groups()
# ... (Do more with matched values)
text = text[len(match.matched_str):]
# Parse the remainder of text
但是,我不知道如何获取匹配字符串的长度,就像我的match.matched_str 伪代码一样。我在 python 的 re 文档中没有看到任何内容。有没有更好的方法来进行这种类型的解析?
【问题讨论】: