[bigdata-041] python3+re 正则表达式 手机号微信号qq号
发布时间:2020-12-25 03:43:12 所属栏目:大数据 来源:网络整理
导读:import reREGEX_PHONE = re.compile(r'1d{10}',re.IGNORECASE)REGEX_QQ = re.compile(r'[1-9]d{4,10}',re.IGNORECASE)REGEX_WX1 = re.compile(u'微信[w,-]{1,20}'.encode('utf8'),re.IGNORECASE)#正则手机号码def get_all_phone_num(s1): global REGEX_PH
import re REGEX_PHONE = re.compile(r'1d{10}',re.IGNORECASE) REGEX_QQ = re.compile(r'[1-9]d{4,10}',re.IGNORECASE) REGEX_WX1 = re.compile(u'微信[w,-]{1,20}'.encode('utf8'),re.IGNORECASE) #正则手机号码 def get_all_phone_num(s1): global REGEX_PHONE return re.findall(REGEX_PHONE,s1) #正则qq号码 def get_all_qq_num(s1): global REGEX_QQ return re.findall(REGEX_QQ,s1) #正则微信号码 def get_all_weixin_num(s1): global REGEX_WX1,REGEX_WX2,REGEX_WX3 ret = [] res = re.findall(REGEX_WX1,s1.encode('utf8')) #易读性是第一位的,代码长不要紧 for i in res: ret.append(i.decode('utf-8')[2:]) return ret #一个函数获取全部信息 def get_phone_qq_wexin(s1): ret = {} ret['mobile']=get_all_phone_num(s1) ret['qq'] = get_all_qq_num(s1) ret['weixin'] = get_all_weixin_num(s1) ret['count'] = len(ret['mobile'])+len(ret['qq'])+len(ret['weixin']) return ret #测试微信号码 # ret = get_all_weixin_num(u'我的微信cc-xf889测试我的微信号dd_tt我的微信号码eeff998') # print(ret) #!/usr/bin/env python3 #!-*- coding:utf-8 -*- import re #手机号码 """ 1、手机号位数为11位; 2、第一位是1,后面是任意10个数字 则表达式为: ”1d{9} """ regex = re.compile(r'1d{10}',re.IGNORECASE) #测试手机号码 phonenums = re.findall(regex,"我的wf;aef18717917631dsl13818373801测试") print(phonenums) #qq号码 """ 1、首先扣扣号开头不能为0; 2、QQ号必须大于5且小于11(或12,13,QQ号最长位); 则正则表达式为: “[1-9]d{4,10}" """ regex = re.compile(r'[1-9]d{4,re.IGNORECASE) qqnums = re.findall(regex,"我的qq261955036测试261955039继续测试") print(qqnums) #微信号码 """ 1. 微信号可能是手机号可能qq号码也可能是一串字符串 2. 开头是"微信",后面有“号”或者没“号”,然后是若干字母 """ regex = re.compile(u'微信[w,re.IGNORECASE) res = re.findall(regex,u'我的微信cc-xf889测试我的微信号dd_tt我的微信号码eeff998'.encode('utf8')) for i in res: print(i.decode('utf-8')) regex = re.compile(u'微信号[w,u'我的微信cc-xf889测试我的微信号dd_tt我的微信号码eeff998'.encode('utf8')) for i in res: print(i.decode('utf-8')) regex = re.compile(u'微信号码[w,u'我的微信cc-xf889测试我的微信号dd_tt我的微信号码eeff998'.encode('utf8')) for i in res: print(i.decode('utf-8')) (编辑:淮安站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |