基于python的-使用正则表达式验证手机号并匹配运营商和所述地域

import re
import json


# 将语句中不符合手机号码规则的数字串剔除,保存剩余符合手机号码规则的数字到ns中
def is_phone(n):
    ns = re.findall('\d+', n)  # 提取出word语句中的数字
    new_list = ns[:]  # 拷贝提取出的数字
    for i in new_list:
        if re.match(r'0?(13|14|15|17|18|19)[0-9]{9}', i):  # 判断是否符合普通11位规则的手机号码
            # print("正确的手机号:", i)
            i
        elif re.match("^((\\+86)|(86))?[1][3456789][0-9]{9}$", i):  # 判断是否符合+86规则的手机号码
            # print("正确的手机号:", i[2:15])
            ns.remove(i)
            ns.append(i[2:15])
        else:
            # print("错误的手机号:",i)
            ns.remove(i)
    return ns


# 匹配手机归属地字典,获取需要的信息,并将其按一定格式保存到dics中
def read_Number(n):
    number = open("手机归属地字典.txt", 'r', encoding='utf-8')  # 读取字典
    lines = number.readlines()
    dic = []

    # 遍历获得的数据
    for i in n:
        n = i
        dic.append(n)
        local = n[0:7]
        for line in lines:
            line = line.strip()
            word = line.split("	")
            # print(word[1])
            if local in word[0]:
                dic.append(word[1])
                # print(dic)
            else:
                continue
    a = '{手机号:' + dic[0] + ',所属地区|运营商:' + dic[1] + '}'
    dics = json.dumps(a, ensure_ascii=False)
    return dics


# def cutNumber(ch):
# text = re.findall('\d+', ch)
# return text

if __name__ == "__main__":
    word = "我的电话号码是13945678415,+8615321354556,qq号547749998"
    print('input:\n%s' % word)
    # w =cutNumber(word)
    number = is_phone(word)
    print('output:')
    for i in number:
        w_ = []
        w_.append(i)
        print(read_Number(w_))

猜你喜欢

转载自blog.csdn.net/fort110/article/details/81240823