正则表达式判断IP地址(python)
分情况:
1位数:0 | 1-9
2位数:[1-9][0-9]
3位数:1开头:1[0-9]{2}
2开头:2[0-4][0-9] | 25[0-5]
1、2位数:0 | [1-9][0-9]?
3位数:1[0-9]{2} | 2[0-4][0-9] | 25[0-5]
import repattern = re.compile(r'^(0|[1-9]\d?|1[0-9]{2}|2[0-4]\d|25[0-5])'r'(\.(0|[1-9]\d?|1[0-9]{2}|2[0-4]\d|25[0-5])){3}$')
while True:r = input('')if r == 'exit':breakif pattern.match(r):print('yes')else:print('no')