【发布时间】:2012-04-22 14:45:25
【问题描述】:
在下面的 ip 地址验证中,我想看看它是否是一个有效的 ip 地址,我该如何使用下面的 re
>>> ip="241.1.1.112343434"
>>> aa=re.match(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}[^0-9]",ip)
>>> aa.group()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
【问题讨论】:
-
正则表达式是检查 IP 地址的糟糕方法。您必须匹配最多 255 的数字,但不是 256,因此正确执行此操作的正则表达式相当大