1. 程式人生 > >python3 判斷字符串是否為IP

python3 判斷字符串是否為IP

pre else color bin div python3 odi spa Coding

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import re

ip = "192.168.1.1"

ip = re.findall("^(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[1-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)$", ip)
print(ip)

if not ip:
    print("no")
else:
    print("yes
")

python3 判斷字符串是否為IP