需求:匹配出0-100之间的数字
#coding=utf-8
import re
ret = re.match("[1-9]?\d","8")
ret.group()
ret = re.match("[1-9]?\d","78")
ret.group()
# 不正确的情况
ret = re.match("[1-9]?\d","08")
ret.group()
# 修正之后的
ret = re.match("[1-9]?\d$","08")
ret.group()
# 添加|
ret = re.match("[1-9]?\d$|100","8")
ret.group()
ret = re.match("[1-9]?\d$|100","78")
ret.group()
ret = re.match("[1-9]?\d$|100","08")
ret.group()
ret = re.match("[1-9]?\d$|100","100")
ret.group()
运行结果:
需求:匹配出163、126、qq邮箱之间的数字
#coding=utf-8
import re
ret = re.match("\w{4,20}@163\.com", "test@163.com")
ret.group()
ret = re.match("\w{4,20}@(163|126|qq)\.com", "test@126.com")
ret.group()
ret = re.match("\w{4,20}@(163|126|qq)\.com", "test@qq.com")
ret.group()
ret = re.match("\w{4,20}@(163|126|qq)\.com", "test@gmail.com")
ret.group()
运行结果: