Five, regular common case: judge whether a mailbox 163 compliant, group

Five, regular common case: judge whether a mailbox 163 compliant, group

1, requires: 4-20 must have entered the mailbox digits before the @, @ and ending 163.com

import re

def main():
	email = input("请输入邮箱:")
	#   "."在此处会有其他含义,需要加上\进行转义,回归原本的意思
	ret = re.match(r"[a-zA-Z0-9_]{4, 20}@163\.com$", email)
	if ret:
	print("输入正确")
	else:
	print("输入的邮箱格式有误")

if __name__ == "__main__":
	main()

2, requires: 4-20 must have entered the mailbox digits before the @ and @ to @ or ending 163.com 126.com

import re

def main():
	email = input("请输入邮箱:")
	#   "|"有或的意思,加上可以多一个选择,再加上()可以具体区分或的范围
	ret = re.match(r"[a-zA-Z0-9_]{4, 20}@(163|126)\.com$", email)
	if ret:
	print("输入正确")
	else:
	print("输入的邮箱格式有误")

if __name__ == "__main__":
	main()

3, taken out of the required type of mail, i.e., 126 or 163

import re

def main():
	email = input("请输入邮箱:")
	#  加上()可以指定取出的内容
	ret = re.match(r"([a-zA-Z0-9_]{4, 20})@(163|126)\.com$", email)
	if ret:
	print("输入正确")
	print("邮箱的@前面部分为:%s" % ret.group(1))	# 取出第一个括号中匹配到的内容
	print("邮箱的类型为:%s" % ret.group(2))	# 取出第二个括号中匹配到的内容
	else:
	print("输入的邮箱格式有误")

if __name__ == "__main__":
	main()
Published 47 original articles · won praise 74 · views 7903

Guess you like

Origin blog.csdn.net/Jacky_kplin/article/details/104744992