【发布时间】:2017-12-27 23:23:59
【问题描述】:
是否可以将这些模式简化为一种模式?
PRIV_LOC = re.compile("^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
PRIV_24 = re.compile("^10\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
PRIV_20 = re.compile("^192\.168\.\d{1,3}.\d{1,3}$")
PRIV_16 = re.compile("^172.(1[6-9]|2[0-9]|3[0-1]).[0-9]{1,3}.[0-9]{1,3}$")
def is_ip_private(ip):
return PRIV_LOC.match(ip) or PRIV_24.match(ip) or PRIV_20.match(ip) or PRIV_16.match(ip)
【问题讨论】:
-
你到底想得到什么
-
我正在尝试确定该地址是否为私人地址。
-
你清楚地知道如何在正则表达式中做 OR,所以它当然可以组合。但对可读性的影响可能没有帮助。
-
jonrsharpe 我同意你的看法,我不会管它的,谢谢
标签: python regex performance