简体   繁体   中英

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

I am using python to parse Postfix logfiles. I need to match lines containing any of multiple patterns, and extract IP address if line matches

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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