本期主题:
python的正则表达式
往期链接:
正则表达式是按照一定的规则来匹配字符串,如果规则对应上了,我们就认为这个字符串匹配上了
确认一个字符串是不是email地址,看是否有@和.com字符串的存在:
import re
email = re.compile(r'(.*)@(.*)\.com')
def is_valid_email(addr):
match_obj = email.match(addr)
if match_obj:
print("get email, groups are ", match_obj.groups())
else:
print("this is not vaild addr: ", addr)
if __name__=="__main__":
is_valid_email('someone@gmail.com')
is_valid_email('bill.gates@microsoft.com')
is_valid_email('test#fff.com')
测试结果:
get email, groups are ('someone', 'gmail')
get email, groups are ('bill.gates', 'microsoft')
this is not vaild addr: test#fff.com