我有一个简单的python脚本来解析日志文件,如果行匹配定义的模式,则提取有问题的IP地址。
以下是我的示例模式:
PATTERNS = [
'warning: Connection [a-zA-Z0-9_]+ 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: User unknown in local recipient table; .*'
]
PATTERNS_COMPILED = re.compile("^(?:" + "|".join(PATTERNS) + ")")
问题是,线条太长。我想在外部定义IP匹配正则表达式并将其用作我的PATTERN匹配
中的变量如果我将IP定义为:
IP = r'[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
我怎样才能在我的PATTERN表达式中引用它?
(我不想匹配字符串IP
。我想匹配变量IP的值)
答案 0 :(得分:0)
您可以通过在匹配的字符串中插入变量IP
来完成此操作:
IP = r'[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
PATTERNS = [
'warning: Connection [a-zA-Z0-9_]+ limit exceeded: [0-9]+ from .*\[(' + IP + ')\] for service smtp',
'NOQUEUE: reject: RCPT from .*\[(' + IP + ')\]: .* Relay access denied; .*',
'NOQUEUE: reject: RCPT from .*\[(' + IP + ')\]: .* Recipient address rejected: User unknown in local recipient table; .*'
]