Discuss / Python / 两道练习题答案

两道练习题答案

Topic source
import re

regex = r'^(<\w[\s\w]+>\s)?(\w+[\w+.]*@\w+.(org|com)$)'
# 题目一:正则匹配
m = re.compile(regex)
# 纯Email地址
if m.match('someone@gmail.com'):
    print('match someone@gmail.com')
if m.match('bill.gates@microsoft.com'):
    print('match bill.gates@microsoft.com')
# 带名字的Email地址
if m.match('<Tom Paris> tom@voyager.org'):
    print('match <Tom Paris> tom@voyager.org')
# 题目二:提取带名字的Email地址
m_email = m.match('<Tom Paris> tom@voyager.org').group(2)
print(m_email)

详解请关注我的CSDN博客: http://blog.csdn.net/sunflowerduidui/article/details/51489394

houbo111

#2 Created at ... [Delete] [Delete and Lock User]

我试了一下,不管是''还是r''里面如果不是.而是.的话,就表示的是任意字符,而不是小数点的意思。把.com换成;com也满足


  • 1

Reply