python正则匹配手机号和车牌号

  • Post author:
  • Post category:python




匹配手机号

res = re.match('^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$',"13145627631")
if res:
    print("匹配到手机号,敏感!")
else:
    print("没匹配到手机号,安全。")



匹配车牌号

car_search = r'[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁台琼使领军北南成广沈济空海]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂领学警港澳]{1}(?!\d)'
text="我的车牌号是京A66661,粤S1E7F2"  #需要抽取的文本
def car_ID_extract(text):
    all_car_id = re.findall(car_search, text)
    car_id = []
    car_id1 = ""
    if all_car_id:
        for i in all_car_id:
            if not i in car_id:
                car_id.append(i)
        for i in car_id:
            car_id1 = car_id1 + ' ' + "".join(tuple(i))   #将列表转字符串
    return car_id1            #返回字符串
print(car_ID_extract(text))



版权声明:本文为qq_44930903原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。