【问题标题】:Find word with Python "Regex" [closed]使用 Python“正则表达式”查找单词 [关闭]
【发布时间】:2023-03-25 06:25:02
【问题描述】:

你能帮忙吗: 我想使用 python 从该文本中获取地址:

 [RIPH] harambe protocol

First alert listed in this telegram group 637 seconds before anyone else.

Coin name:    harambe protocol
Address:         0x10964C2ffDEA1e99B5e26D102516d9b03368915f
Platform:        BSC
Time:               08:02:58 UTC

【问题讨论】:

    标签: python regex


    【解决方案1】:

    请您尝试以下方法:

    #!/usr/bin/python
    
    import re
    
    with open('textfile') as f:
        for line in f:
            m = re.search(r'Address:\s*(0[xX][0-9A-Fa-f]+)', line)
            if (m):
                print(m.group(1))
    

    输出:

    0x10964C2ffDEA1e99B5e26D102516d9b03368915f
    

    【讨论】:

    • 不是文本文件
    • 它只是代表文件名。将文件名更改为您拥有的任何名称。
    • 我使用了该代码但什么也没返回:
    • m = re.search("地址:\s*(0[xX][0-9A-Fa-f]+)", text) if m: print(m.group(1 ))
    • 感谢您的帮助
    【解决方案2】:

    试试这个:

    import re
    
    pattern = "Address:.*(0x.*)"
    
    address = re.findall(pattern, your_text, re.MULTILINE)
    
    print(address)
    
    

    【讨论】:

    • 以及如何只打印地址?
    • 这个正则表达式只接受地址:address = re.findall(pattern, your_text, re.MULTILINE);打印(地址)
    • 它返回'[]'
    • 把模式改成这样:pattern = "Address:(.*)"
    • 我得到 [' ** '0xi0964C2fEDEfte99B5e26D102516d9b033689151"]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-14
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    相关资源
    最近更新 更多