在python中使用regex进行Dhcpd租约匹配?

2024-09-28 22:22:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想匹配所有的dhcp租约已经给了mac地址。在

我写了这个代码

fh = open(leaseFile)
lines = fh.read()
fh.close()
regex = r"lease\s*[0-9\.]+\s*\{[^\{\}]*%s[^\{\}]*?\}" % mac #mac comes as parameter
m = re.findall(regex,lines,re.DOTALL)

如果租约不包含'}'字符,这很有效。但如果是这样,我的正则表达式就失败了。 例如:

^{pr2}$

我不知道如何处理这个异常。谢谢你的建议。。。在

编辑

经过研究,我决定在多行模式下使用这个正则表达式。我试过的所有租约都有效。在

fh = open(leaseFile)
lines = fh.read()
fh.close()
regex = r"lease\s*[0-9\.]+\s*\{[^\{\}]*%s[\s\S]*?^\}" % mac #mac comes as parameter
m = re.findall(regex,lines,re.MULTILINE)

Tags: reclosereadparametermacasopenregex