python + 正则表达式

import re
a,b,c,d,e,wrong,privite = 0,0,0,0,0,0,0
while True:
    try:
        string = input().split('~')
        #判断子网掩码是否有效,匹配三种有效格式
        #code = ['254','252','248','240','224','192','128','0']
        # 255.code.0.0   255.255.code.0   255.255.255.code
        if re.match('^255\.(254|252|248|240|224|192|128|0)\.0\.0$', string[1]) or re.match('^255\.255\.(254|252|248|240|224|192|128|0)\.0$', string[1]) or re.match('^255\.255\.255\.(254|252|248|240|224|192|128|0)$', string[1]):
            #掩码合法,判断abcde
            if re.match('^([1-9]\d?|1[0-1]\d|12[0-6])\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])$', string[0]):
                a += 1
                #私有10.*.*.*
                if re.match('^10\.', string[0]):
                    privite += 1
                continue
            elif re.match('^(12[8-9]|1[3-8]\d|19[0-1])\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])$', string[0]):
                b += 1
                #私有172.16-31.*.*
                if re.match('^172\.(1[6-9]|2\d|3[0-1])\.', string[0]):
                    privite += 1
                continue
            elif re.match('^(19[2-9]|2[0-1]\d|22[0-3])\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])$', string[0]):
                c += 1
                #私有192.168.*.*
                if re.match('^192\.168\.', string[0]):
                    privite += 1
                continue
            elif re.match('^(22[4-9]|23\d)\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])$', string[0]):
                d += 1
                continue
            elif re.match('^(24[0-9]|25[0-5])\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])$', string[0]):
                e += 1
                continue
            #既不属于abcde又不属于不合法(也就是掩码合法),直接continue忽略
            elif re.match('^(0|127)\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])$', string[0]):
                continue
            else:
                #掩码合法,IP地址错误
                wrong += 1
                continue
        else:
            #掩码不合法,错误+1
            wrong += 1
            continue
    except:
        break
print(a,b,c,d,e,wrong,privite)