1. 程式人生 > >正則匹配出ip

正則匹配出ip

src = “security/afafsff/?ip=123.4.56.78&id=45”
請寫一段程式碼用正則匹配出ip
匹配ip地址的python正則表示式

src = "security/afafsff/?ip=123.4.56.78&id=45"
result = re.findall(r'([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])', src)
print result
[('123', '4', '56', '78')]