我想匹配所有的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)
这应该能奏效。在
相关问题 更多 >
编程相关推荐