使用python正则表达式校验ipv4地址代码如下:
#!/usr/bin/python2.7
# -*- coding:utf-8 -*-
import re
def ipv4_addr_check(ipAddr):
p = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$')
if p.match(ipAddr):
return True
else:
return False
print ipv4_addr_check("120.112.112.110") # 输出结果为True
print ipv4_addr_check("172.31.137.256") # 输出结果为False