python正则表达式匹配中的引用变量

时间:2016-03-14 08:47:24

标签: regex python-2.7

我有一个简单的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的值)

1 个答案:

答案 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; .*'
]