正则匹配护照格式

美国、英国护照号码:9 位数字 加拿大护照:2 个字母加 6 个数字 法国护照:2 个数字加 2 个字母加 5 个数字 意大利护照:2 个字母加 7 个数字 西班牙护照:3 个字母加 6 个数字 德国护照:9 个字符,由 2-3 个数字与 6-7 个字母混排,头尾是字母。或者单纯为 9 个数字 

def foreign_passport(sentence):
    pattern = [r'\d{9}', r'[a-zA-Z]{2}[0-9]{6}', r'[0-9]{2}[a-zA-Z]{2}[0-9]{5}', r'[a-zA-Z]{2}[0-9]{7}', r'[a-zA-Z]{3}[0-9]{6}', r'[a-zA-Z][a-zA-Z0-9]{7}[a-zA-Z]']
    categories = {0:"美国/英国/德国", 1:"加拿大/意大利/西班牙", 2:"法国", 3:"意大利", 4:"西班牙", 5:"德国"}
    # print(re.findall(pattern[5], sentence, re.I))
    for i in range(len(pattern)):
        if re.search(pattern[0], sentence, re.I): #re.I表示忽略字母大小写
            return categories[0] + '护照', re.findall(pattern[0], sentence, re.I)
        elif re.search(pattern[1], sentence, re.I): #re.I表示忽略字母大小写
            count_alpha = 0
            count_num = 0
            for s in re.findall(pattern[1], sentence, re.I)[0]:
                if s.isalpha():
                    count_alpha += 1
                elif s.isdigit():
                    count_num += 1
            # print('alpha',count_alpha)
            # print('num', count_num)
            if count_alpha == 2 and count_num == 6:
                return categories[1] + '护照', re.findall(pattern[1], sentence, re.I)
            elif count_alpha == 2 and count_num == 7:
                return sentence + '   字段中含有' + categories[3] + '护照'
            elif count_alpha == 3 and count_num == 6:
                return sentence + '   字段中含有' + categories[4] + '护照'
        elif re.search(pattern[2], sentence, re.I): #re.I表示忽略字母大小写
            return categories[2] + '护照', re.findall(pattern[2], sentence, re.I)
        elif re.search(pattern[3], sentence, re.I): #re.I表示忽略字母大小写
            return categories[3] + '护照', re.findall(pattern[3], sentence, re.I)
        elif re.search(pattern[4], sentence, re.I): #re.I表示忽略字母大小写
            return categories[4] + '护照', re.findall(pattern[4], sentence, re.I)
        elif re.search(pattern[5], sentence, re.I): #re.I表示忽略字母大小写
            return categories[5] + '护照', re.findall(pattern[5], sentence, re.I)
        else:
            return '字段中不含护照信息'

 

 

你可能感兴趣的:(数据,正则表达式,Python)