LinuxEye - Linux系统教程

LinuxEye - Linux系统教程

当前位置: 主页 > 脚本编程 >

Python正则匹配邮箱和电话号码

时间:2013-06-15 15:54来源:wubiaoblog.com/archives/9 编辑:吴飚 点击:
同时匹配邮箱和电话号码的正则表达式,要想很完美的匹配,挺复杂的。各邮箱提供商的标准都多少有些许差别。如: 163:6-18个字符,可使用字母、数字、下划线,需以字母开头。 gma
同时匹配邮箱和电话号码的正则表达式,要想很完美的匹配,挺复杂的。各邮箱提供商的标准都多少有些许差别。如:
163:6-18个字符,可使用字母、数字、下划线,需以字母开头。
gmail:可以使用字母、数字和英文句点,不能使用除字母 (a-z)、数字和英文句号外的其他字符,下划线也是不可以的。
yahoo:4至32个字符(包括字母、数字、下划线、单点),且必须以英文字母开头。
sina:4-16个字符,支持英文小写、数字、下划线,不支持全部为数字或下划线。
qq:由3-18个英文、数字、点、减号、下划线组成。
其他的提供商还有更多格式标准。

而对于电话号码:
国内固定电话城市区号,有三位的,也有四位的。
固定电话号码有七位的,也有八位的。
手机号码包括:
中国移动:134、135、136、137、138、139、147、150、151、152、157、158、159、182、187、188。
中国联通:130、131、132、155、156、185、186
中国电信:133,153,,180,189
如果再加上国外的号码的话,要考虑的更多。

要想都匹配并且都是有效的话,挺麻烦的,这里的匹配不能算完美,但也考略了不少的场景。
如果要写的很完美的话,我觉得要加一些逻辑判断。
比如说,对邮箱和电话号码判断是哪家提供商的。
import re

print 'Please input mail address:',
inputmail=raw_input()

"""
说明:
[^\._-][\w\.-]+@(?:[A-Za-z0-9]+\.)+[A-Za-z]+$匹配邮箱。
综合目前国内常用的邮箱,大概通用的规则包括:
1、[^\._],不能以下划线和句点开头。
2、[\w\.]+,包括字母、数字。而对句点及下划线各提供商有差别,对此有效性不做更严格的判断。
3、@是必须的。
4、(?:[A-Za-z0-9]+\.)+[A-Za-z]+$,@后以xxx.xxx结尾,考虑到多级域名,会有这种情况xxx.xxx.xxx如xxx@yahoo.com.cn

^0\d{2,3}\d{7,8}$|^1[358]\d{9}$|^147\d{8}$匹配电话号码。
只考虑国内的情况,大概通用的规则包括:
1、^0\d{2,3},固定电话区号3-4位数字,以0开头。
2、d{7,8}$,固定电话一般7-8位数字。
3、国内目前的手机号码都是11位数字,三大运营商的号码段基本都在上面列出来了,我们这里除了147的号码的段,其他的都只考虑前两位,
第三位就不考虑了,否则,工作量也很大。前两位包括13*、15*、18*。

"""
p=re.compile('[^\._-][\w\.-]+@(?:[A-Za-z0-9]+\.)+[A-Za-z]+$|^0\d{2,3}\d{7,8}$|^1[358]\d{9}$|^147\d{8}

)

match = p.match(inputmail)

if match:
    print match.group()
else:
    print 'mail address or phone number is error!'

"""
部分测试结果:

正确的邮箱地址格式:
Please input mail address: biao.wu@gmail.com
biao.wu@gmail.com

Please input mail address: wu_biao@163.com
wu_biao@163.com

Please input mail address: wubiao@yahoo.com.cn
wubiao@yahoo.com.cn

Please input mail address: wu-biao@qq.com
wu-biao@qq.com

Please input mail address: 8888@qq.com
8888@qq.com

错误的邮箱地址格式:
Please input mail address: .biao@163.com
mail address or phone number is error!

Please input mail address: _wubiao@qq.com
mail address or phone number is error!

Please input mail address: -qq@qq.com
mail address or phone number is error!

Please input mail address: biao@@.com
mail address or phone number is error!

Please input mail address: wubiao@qq.com.
mail address or phone number is error!

Please input mail address: wubiao@qq.com.22
mail address or phone number is error!

Please input mail address: wubiao#@163.com
mail address or phone number is error!

正确的电话号码格式:
Please input mail address: 13530315051
13530315051

Please input mail address: 075512345678
075512345678

Please input mail address: 18667676767
18667676767

错误的电话号码格式:
Please input mail address: 135303154
mail address or phone number is error!

Please input mail address: 135303112345
mail address or phone number is error!

Please input mail address: 1234567890
mail address or phone number is error!

"""

转载请保留固定链接: https://linuxeye.com/program/1760.html

------分隔线----------------------------
标签:Python
栏目列表
推荐内容