python学习站 /学习python第三周
阅读主题
正文字体
字体大小

08.homework

本节2423字2025-04-08 19:30:45
"""
Auther:少校
Time:2025/4/7 17:09
越努力,越幸运
"""
from re import *
# 1.验证用户名(仅允许英文、数字、下划线)
str1="zhangsanfeng_9"
print(fullmatch(r"[A-Za-z\d_]+",str1))
# 2.验证包含汉字、英文、数字的用户名
str1="张三丰a8"
print(fullmatch(r"[A-Za-z\d\u4e00-\u9fa5]+",str1))
# 3.匹配6-18位的字母数字组合密码
str1="9ad8d5a8"
print(fullmatch(r"[A-Za-z\d]{6,18}",str1))
# 4.验证QQ号(5-12位数字,不以0开头)
str1="374548"
print(fullmatch(r"[1-9]\d{4,11}",str1))
# 5.匹配标准邮箱地址(如name@example.com)
str1="zhang_san9854@qq.com"
print(fullmatch(r"[A-Za-z\d_]+@[a-z\d_]+\.(com|cn|net|org|cc|vip|top)",str1))
# 6.匹配身份证号码(15位或18位,校验最后一位)
str1="44422219990402003x"
print(fullmatch(r"[1-9]\d{13}[\dXx]|[1-9]\d{16}[\dXx]",str1))
# 7.匹配IP地址(如192.168.1.1)
str1="192.168.1.1"
# \d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]
print(fullmatch(r"((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])",str1))
# 8.匹配浮点数(如3.14或-0.5)
str1="3.14"
# (0|[1-9]\d*)\.\d+
print(fullmatch(r"[+-]?(0|[1-9]\d*)\.\d+",str1))
# 9.验证金额格式(如$1,023,032.03)
str1="$0.03"
print(fullmatch(r"\$(([1-9]\d{0,2}(,\d{3})*|0)\.\d{2})",str1))
# 10.匹配日期格式yyyy-mm-dd(考虑闰年)
str1="2000-2-29"
year,month,day=str1.split("-")
is_year=fullmatch(r"\d{4}",year) is not None
is_month=fullmatch(r"0[1-9]|1[0-2]",month) is not None
if is_year and is_month:
    month = int(month)
    if month in (1,3,5,7,8,10,12):
        is_day= fullmatch(r"0[1-9]|[12]\d|3[01]",day)
    elif month == 2:
        year = int(year)
        if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
            is_day = fullmatch(r"0[1-9]|[12]\d",day)
        else:
            is_day = fullmatch(r"0[1-9]|1\d|2[0-8]",day)
    else:
        is_day = fullmatch(r"0[1-9]|[12]\d|30")
    if is_day:
        print("合法")
    else:
        print("不合法")
else:
    print("不合法")

print(fullmatch(
    r'[1-9]\d{0,3}-[13578]|10|12-[1-9]|[1-2]\d|3[0-1]|([1-9]\d{0,3}-([469]|(11))-([1-9]|([1-2]\d)|(30)))|([1-9]\d{0,3}-2-(([1-9])|([1-2]\d)))',
    str1))
# 11.匹配非零的正整数。
#\+?[1-9]\d*
str1="d+387,adaeh333.56dasd-78.9"
print(findall(r"\b\d+\b",str1))

# 12.提取文本中的手机号和邮箱
str1="15238977033ddaopsdopi张三丰d_@qq.com"
print(findall(r"[1-9][3-9]\d{9}|[A-Za-z\d_]+@[a-z\d_]+\.com|cn|net|org|cc|vip|top",str1))

# 13.删除字符串中所有的汉字
str1="alksdjlass张三丰asdo"
print(sub(r"[\u4e00-\u9fa5]*", "", str1))
# 14.脏话屏蔽,将输入的内容中的不文明用于全部替换成*
str1="alk2ss bd3jlass张三丰asd1o"
print(sub(r"[123]|张三丰|赵无忌|s\s*b", "*", str1))


网友评论

相关作品