[bigdata-041] python3+re 正则表达式 手机号微信号qq号

来源:互联网 发布:婚礼设计软件 编辑:程序博客网 时间:2024/05/01 12:15
import reREGEX_PHONE = re.compile(r'1\d{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个数字则表达式为:    ”1\d{9}"""regex = re.compile(r'1\d{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,10}', re.IGNORECASE)qqnums = re.findall(regex,"我的qq261955036测试261955039继续测试")print(qqnums)#微信号码"""1. 微信号可能是手机号可能qq号码也可能是一串字符串2. 开头是"微信",后面有“号”或者没“号”,然后是若干字母"""regex = re.compile(u'微信[\w,-]{1,20}'.encode('utf8'), 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,-]{1,20}'.encode('utf8'), 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,-]{1,20}'.encode('utf8'), re.IGNORECASE)res = re.findall(regex, u'我的微信cc-xf889测试我的微信号dd_tt我的微信号码eeff998'.encode('utf8'))for i in res:    print(i.decode('utf-8'))


0 0
原创粉丝点击