利用正则表达式完成下面的操作:
1.用户名匹配
要求: 1.用户名只能包含数字 字母 下划线
2.不能以数字开头
3.⻓度在 6 到 16 位范围内
from re import fullmatch
re_str = '[a-zA-Z_][a-zA-Z_\d]{5,15}'
user_name = fullmatch(re_str, 'hdvjhjA')
print(user_name)
- 密码匹配
要求: 1.不能包含!@#¥%^&*这些特殊符号
2.必须以字母开头
3.⻓度在 6 到 12 位范围内
from re import fullmatch
re_str = '[a-zA-Z][^!@#¥%^&*]{5,11}'
pass_word = fullmatch(re_str, 'hdvjhjA')
print(pass_word)
- ipv4 格式的 ip 地址匹配
提示: IP地址的范围是 0.0.0.0 - 255.255.255.255
from re import fullmatch
ip =r'((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}'
result = fullmatch(ip, '45.56.67.78')
print(result)
- 提取用户输入数据中的数值 (数值包括正负数 还包括整数和小数在内) 并求和
例如:“-3.14good87nice19bye” =====> -3.14 + 87 + 19 = 102.86
from re import findall
message = r'[+-]?\d+[.]?\d+'
num = findall(message, '-3.14good87nice19bye')
sum1 = 0
for x in num:
sum1 += float(x)
print(sum1)
-
验证输入内容只能是汉字
re_str = r'\u4e00-\u9fa5'
-
匹配整数或者小数(包括正数和负数)
re_str = r'[+-]?\d[.]?\d*'