1.写一个正则表达式判断一个字符串是否是ip地址
from re import *
def verify_ip(value: str):
return fullmatch(r'((\d\d?|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d\d?|1\d\d|2[0-4]\d|25[0-5])', value)
v1 = verify_ip('255.189.10.37')
v2 = verify_ip('256.189.89.9')
print(v1)
print(v2)
2计算一个字符串中所有的数字的和 例如:字符串是:‘hello90abc 78sjh12.5’ 结果是90+78+12.5 = 180.5
from re import *
def numbers_sum(value: str):
numbers_list = findall(r'\d+\.?\d*', value)
sum_num = 0
for number in numbers_list:
sum_num += float(number)
return sum_num
string = numbers_sum('hello90abc 78sjh12.5')
print(string)
3.验证输入的内容只能是汉字
while True:
value = input('请输入要验证的内容:')
str1 = fullmatch(r'[\u4e00-\u4f9a]+', value)
if str1 is None:
print('输入内容不合法,请输入汉字')
else:
break
4.电话号码的验证
tel = fullmatch(r'[1][0-9]{10}', '13557269820')
print(tel)