简体   繁体   English

Python:匹配多个正则表达式模式之一,并在匹配时提取IP地址

[英]Python: match one of multiple regex patterns and extract IP address if match

I am using python to parse Postfix logfiles. 我使用python来解析Postfix日志文件。 I need to match lines containing any of multiple patterns, and extract IP address if line matches 我需要匹配包含多个模式中的任何一个的行,并在行匹配时提取IP地址

ip = re.search('^warning: Connection rate limit exceeded: [0-9]* from .*\[([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\] for service smtp', message)
if not ip:
    ip = re.search('^NOQUEUE: reject: RCPT from .*\[([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\]: .*: Relay access denied; .*', message)
    if not ip:
        ip = re.search('^NOQUEUE: reject: RCPT from .*\[([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\]: .*:  Recipient address rejected: .*', message)
...
...
print ip.group(1)

Any line will only ever match one pattern. 任何行只能匹配一个模式。 I know that normaly I can use '(pattern1|pattern2|pattern3)' to match any of multiple patterns, but since I am alredy using parenthesis () to group the IP address which I want to extract, I don't know how to do that. 我知道,我可以使用'(pattern1 | pattern2 | pattern3)'来匹配任何多个模式,但由于我使用括号()来分组我要提取的IP地址,我不知道如何去做。

I will have quite a lot of patterns to match. 我将有相当多的模式匹配。 What would be the most clean/elegant way to do it ? 什么是最干净/优雅的方式呢?

You can use a non-capturing group : 您可以使用非捕获组

patterns = [
    "warning: Connection rate limit exceeded: [0-9]* from .*\[([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\] for service smtp",
    "NOQUEUE: reject: RCPT from .*\[([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\]: .*: Relay access denied; .*",
    "NOQUEUE: reject: RCPT from .*\[([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\]: .*:  Recipient address rejected: .*"
]
pattern = re.compile("^(?:" + "|".join(patterns) + ")")
ip = pattern.search(message)

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

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