pythonpython爬虫正则表达式式分析文本文件中所有的e-mail地址

python正则提取文本内容,或者其他方法
[问题点数:40分,结帖人taxuelingbing]
python正则提取文本内容,或者其他方法
[问题点数:40分,结帖人taxuelingbing]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
2006年5月 PHP大版内专家分月排行榜第二2006年4月 PHP大版内专家分月排行榜第二2007年1月 PHP大版内专家分月排行榜第二
2013年10月 其他开发语言大版内专家分月排行榜第三2007年2月 PHP大版内专家分月排行榜第三
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。算法(336)
Python(80)
正则表达式
首先来个简单的例子,利用Python实现匹配163邮箱的代码:
#-*- coding:utf-8 -*-
__author__ = '杨鑫'
text = input(&Please input your Email address:\n&):
if re.match(r'[0-9a-zA-Z_]{0,19}@',text):
print('Email address is Right!')
print('Please reset your right Email address!')
接着来一个匹配所有邮箱格式的代码:
#-*- coding:utf-8 -*-
__author__ = '杨鑫'
text = input(&Please input your Email address:\n&)
if re.match(r'^[0-9a-zA-Z_]{0,19}@[0-9a-zA-Z]{1,13}\.[com,cn,net]{1,3}$',text):
#if re.match(r'[0-9a-zA-Z_]{0,19}@',text):
print('Email address is Right!')
print('Please reset your right Email address!')
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:472136次
积分:14494
积分:14494
排名:第548名
原创:992篇
转载:24篇
评论:145条
文章:41篇
阅读:9932
文章:18篇
阅读:9162
文章:57篇
阅读:35350
文章:104篇
阅读:67747
文章:49篇
阅读:32080
文章:18篇
阅读:13281
文章:13篇
阅读:9612
文章:123篇
阅读:56626
(14)(42)(44)(16)(25)(13)(22)(27)(14)(4)(2)(17)(15)(11)(21)(31)(83)(65)(34)(29)(80)(79)(75)(43)(9)(52)(37)(45)(42)(34)(1)在Python中使用正则表达式同时匹配邮箱和电话并进行简单的分类
在Python使用正则表达式需要使用re(regular exprssion)模块,使用正则表达式的难点就在于如何写好pile(r' 正则表达式')的内容。
下面是在Python中使用正则表达式同时匹配邮箱和电话并进行简单的分类的代码,本文只进行了简单的分类,读者可使用补充部分提供的信息进行详细分类。
pile(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):
& & m=p.match(s)
& & if m==None:
& & & & print 'mail address or phone number is wrong'
& & else: & & & &
& & & & 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.append('tju_')
& & s.append('123@')
& & s.append('123456@')
& & s.append('+6')
& & s.append('')
& & for i in range(len(s)):
& & & & mail_or_phone(p,s[i])
结果如下:
It is 163 mail,the address is tju_
It is tju mail,the address is 123@
It is qq mail,the address is 123456@
It is mobilephone number,the number is +6
It is telephone number,the number is
该代码中正则表达式分为三部分:
(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或者@
网易: @、@、@yeah.net
google: @
新浪: @、@
高校: @等
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
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'

我要回帖

更多关于 python 中文文本分析 的文章

 

随机推荐