简体   繁体   English

替换 Python 文件中的特定字符串(之后)

[英]Replace specific string (after) in file in Python

Replace specific string (below) in file in Python替换 Python 文件中的特定字符串(如下)

I have a text file that is something like the following:我有一个类似于以下内容的文本文件:

test   al,al
jne    0x514 <asm4+23>
mov    DWORD PTR [ebp-0x8],0x1
jmp    0x587 <asm4+138>
mov    edx,DWORD PTR [ebp-0x8]

I need the result to be like this我需要结果是这样的

test   al,al
jne    asm4+23
mov    DWORD PTR [ebp-0x8],0x1
jmp    asm4+138
mov    edx,DWORD PTR [ebp-0x8]

This could work for your example.这可能适用于您的示例。 I have used python regular expressions我用过 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)

Hope this helps希望这可以帮助

You can use just ".startswith" method:您可以只使用“.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))

You're converting disassembly into something that can be re-assembled.您正在将反汇编转换为可以重新组装的东西。

Instead of post-processing GDB or objdump -drwC -Mintel output, use a disassembler that outputs in actual GAS or NASM syntax in the first place, with extra info in comments.与其对 GDB 或objdump -drwC -Mintel output 进行后处理,不如使用首先以实际 GAS 或 NASM 语法输出的反汇编程序,并在注释中添加额外信息。 Specifically for x86, Agner Fog's objconv .专门针对 x86, Agner Fog 的objconv

See How to generate assembly code with gcc that can be compiled with nasm for example output.请参阅如何使用可使用 nasm 编译的 gcc 生成汇编代码,例如 output。

(I don't think it can target GAS .intel_syntax , only GAS, NASM/YASM, or MASM. But GAS .intel_syntax is MASM-like except for the directives so that may be useful. Or if you don't mind AT&T syntax, it can be assembled directly without installing NASM.) (我不认为它可以针对 GAS .intel_syntax ,只能针对 GAS、NASM/YASM 或 MASM。但是 GAS .intel_syntax除了指令之外类似于 MASM,因此这可能很有用。或者如果您不介意 AT&T 语法,可以直接组装,无需安装NASM。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM