使用正则从objdump中提取地址和函数调用
我正在努力理解正则表达式,但还是遇到了一些问题。我有这样的输出(来自 objdump -D
):
804869f: e8 cc fe ff ff call 8048570 <socket@plt>
8048713: e8 38 fe ff ff call 8048550 <bind@plt>
8048750: e8 0b fe ff ff call 8048560 <listen@plt>
我想提取出调用发生的地址(第一列)和函数的名称(比如:socket、bind、listen)。
我试过这个:
match = re.match(r' (.*): (.*) <(.*)@plt>', line)
print match.group(1)
print match.group(3)
根据我的理解,我本来期待这个能正常工作。第一个组应该是第一个空格和冒号之间的字符串,第三个组应该是在 <
和 @
之间的内容。但我得到了 AttributeError: 'NoneType' object has no attribute 'group'
的错误。
1 个回答
1
你正在进行一种非贪婪的匹配。
.*
会把所有字符都吃掉,所以如果你提前知道一些信息,最好具体一点。
一个更好的模式可以像下面这样:
st=re.match(r'\s+([0-9A-Fa-f]+):' # Address starting with one or more space
r'\s+.+?' # Skip characters (non-greedy using ?)
r'([0-9A-Fa-f]+)\s+' # Address followed by space
r'<(\S+)@plt>', # function name, anything except space
line)
另外,记得总是检查匹配是否成功。
if st: # Use st or some different variable other then 'match' itself
print st.group(1)
print st.group(2)
print st.group(3)