[英]Replace specific string (after) in file in Python
替換 Python 文件中的特定字符串(如下)
我有一個類似於以下內容的文本文件:
test al,al
jne 0x514 <asm4+23>
mov DWORD PTR [ebp-0x8],0x1
jmp 0x587 <asm4+138>
mov edx,DWORD PTR [ebp-0x8]
我需要結果是這樣的
test al,al
jne asm4+23
mov DWORD PTR [ebp-0x8],0x1
jmp asm4+138
mov edx,DWORD PTR [ebp-0x8]
這可能適用於您的示例。 我用過 python 正則表達式
import re
regex = r"(0.+<)(.+)>"
input_string="""
test al,al
jne 0x514 <asm4+23>
mov DWORD PTR [ebp-0x8],0x1
jmp 0x587 <asm4+138>
mov edx,DWORD PTR [ebp-0x8]
"""
output=re.sub(r"(0.+<)(.+)>", r"\2", input_string)
print(output)
希望這可以幫助
您可以只使用“.startswith”方法:
test = """
test al,al
jne 0x514 <asm4+23>
mov DWORD PTR [ebp-0x8],0x1
jmp 0x587 <asm4+138>
mov edx,DWORD PTR [ebp-0x8]
""".splitlines()
i = 0
while i < len(test):
if test[i].startswith("jne"):
test[i] = "jne 0x514 <asm4+23>"
elif test[i].startswith("jmp"):
test[i] = "jmp 0x587 <asm4+138>"
i += 1
print("\n".join(test))
您正在將反匯編轉換為可以重新組裝的東西。
與其對 GDB 或objdump -drwC -Mintel
output 進行后處理,不如使用首先以實際 GAS 或 NASM 語法輸出的反匯編程序,並在注釋中添加額外信息。 專門針對 x86, Agner Fog 的objconv
。
請參閱如何使用可使用 nasm 編譯的 gcc 生成匯編代碼,例如 output。
(我不認為它可以針對 GAS .intel_syntax
,只能針對 GAS、NASM/YASM 或 MASM。但是 GAS .intel_syntax
除了指令之外類似於 MASM,因此這可能很有用。或者如果您不介意 AT&T 語法,可以直接組裝,無需安裝NASM。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.