做社交网站,做外贸上阿里巴巴什么网站,免费高清logo在线,网站建设费用及预算在Python使用正则表达式需要使用re(regular exprssion)模块#xff0c;使用正则表达式的难点就在于如何写好pre.compile(r 正则表达式)的内容。下面是在Python中使用正则表达式同时匹配邮箱和电话并进行简单的分类的代码#xff0c;本文只进行了简单的分类#xff0c;读者可…在Python使用正则表达式需要使用re(regular exprssion)模块使用正则表达式的难点就在于如何写好pre.compile(r 正则表达式)的内容。下面是在Python中使用正则表达式同时匹配邮箱和电话并进行简单的分类的代码本文只进行了简单的分类读者可使用补充部分提供的信息进行详细分类。import repre.compile(r^[\w\d][\d\w\_\.]([\d\w])\.([\d\w])(?:\.[\d\w])?$|^(?:\86)?(\d{3})\d{8}$|^(?:\86)?(0\d{2,3})\d{7,8}$)def mail_or_phone(p,s):mp.match(s)if mNone:print mail address or phone number is wrongelse:if m.group(1)!None:if m.group(1)vip:print It is %s mail,the address is %s %(m.group(2),m.group())else:print It is %s mail,the address is %s %(m.group(1),m.group())else:if m.group(3)!None:print It is mobilephone number,the number is %s %m.group()else:print It is telephone number,the number is %s %m.group()if __name____main__:s[]s.append(tju_123163.com)s.append(123tju.edu.cn)s.append(123456vip.qq.com)s.append(8615822123456)s.append(0228612345)for i in range(len(s)):mail_or_phone(p,s[i])结果如下It is 163 mail,the address is tju_123163.comIt is tju mail,the address is 123tju.edu.cnIt is qq mail,the address is 123456vip.qq.comIt is mobilephone number,the number is 8615822123456It is telephone number,the number is 0228612345该代码中正则表达式分为三部分(1) ^[\w\d][\d\w\_\.]([\d\w])\.([\d\w])(?:\.[\d\w])?$ 这部分用于匹配邮箱(2) ^(?:\86)?(\d{3})\d{8}$ 这部分用于匹配移动电话(2) ^(?:\86)?(0\d{2,3})\d{7,8}$ 这部分用于匹配固定电话邮箱中后面有的有一个‘.有的有两个‘.’而且有的后面紧挨着的是‘vip’而不是‘qq’等邮箱标识移动电话和固定电话在来电显示中经常出现‘86’所以匹配过程中要注意这一点正则表达式中使用了()进行分组然后可以通过group(i)来获得相应分组的信息来进行判断补充1.常用的邮箱QQ qq.com或者foxmail.com网易 163.com、126.com、yeah.netgoogle gmail.com新浪 sina.com、sina.cn搜狐 sohu.com高校 tju.edu.cn等2.中国三大运营商手机号段移动134、135、136、137、138、139、147、150、152、154、157、158、159、182、183、187、188联通130、131、132、155、156、185、186电信133、153、180、189