如何从Python中的文本中提取单词

San*_*osh 2 python

我有这个字符串"IP 1.2.3.4目前在白名单中受到信任,但它现在使用新的可信证书." 在日志文件中.我需要做的是查找此消息并从日志文件中提取IP地址(1.2.3.4).

import os
import shutil
import optparse
import sys

def main():
    file = open("messages", "r")
    log_data = file.read()
    file.close()

    search_str = "is currently trusted in the white list, but it is now using a new trusted certificate."

    index = log_data.find(search_str)
    print index

    return

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

如何提取IP地址?感谢您的回复.

AlG*_*AlG 5

真的很简单的答案:

msg = "IP 1.2.3.4 is currently trusted in the white list, but it is now using a new trusted certificate."

parts = msg.split(' ', 2)

print parts[1]
Run Code Online (Sandbox Code Playgroud)

结果是:

1.2.3.4
Run Code Online (Sandbox Code Playgroud)

如果你愿意,你也可以做RE,但对于这个简单的事情......