欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

re正则表达式练习

程序员文章站 2022-06-22 14:44:59
判断变量名是否符合要求import re names = ["name1","_name","2_name","__nam!12__","__name__"]for name in names: ret = re.match("^[a-zA-Z_][a-zA-Z1-9_]*$",name) if ret: print("变量名%s符合要求"%ret.group()) else: print("变量名%s非法"%name)结果:impor...

判断变量名是否符合要求

import re 

names = ["name1","_name","2_name","__nam!12__","__name__"]

for name in names:
    ret = re.match("^[a-zA-Z_][a-zA-Z1-9_]*$",name)
    if ret:
        print("变量名%s符合要求"%ret.group())
    else:
        print("变量名%s非法"%name)

结果:

import re 

names = ["name1","_name","2_name","__nam!12__","__name__"]

for name in names:
    ret = re.match("^[a-zA-Z_][a-zA-Z1-9_]*$",name)
    if ret:
        print("变量名%s符合要求"%ret.group())
    else:
        print("变量名%s非法"%name)

判断邮箱是否为QQ,126,136的邮箱,@前4-20个字符

email = input("请输入邮箱:")

# 如果在正则表达式中出现普通字符?.等需要转译
ret = re.match("^([a-zA-Z_0-9]{4,20})@(163|126|qq)\.com$",email)
if ret:
    print("邮箱:%s符合要求"%ret.group(1))
else:
    print("邮箱:%s非法"%email)

结果:

请输入邮箱:123456@qq.com
邮箱:123456符合要求

本文地址:https://blog.csdn.net/qq_28368377/article/details/107475795

相关标签: python基础