在Python使用正则表达式需要使用re(regular exprssion)模块,使用正则表达式的难点就在于如何写好p=re.compile(r' 正则表达式')的内容。
下面是在Python中使用正则表达式同时匹配邮箱和电话并进行简单的分类的代码,本文只进行了简单的分类,读者可使用补充部分提供的信息进行详细分类。
import rep=re.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):
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)!