1.写一个正则表达式判断一个字符串是否是ip地址.规则:一个ip地址由4个数字组成,每个数字之间用.连接。每个数字的大小是0-255
from re import *
0-99:\d\d?
100-199:1\d\d
200-249:2[0-4]\d
200-255:25[0-5]
re1= 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])'
print(fullmatch(re1, '255.189.89.9'))
2.计算一个字符串中所有的数字的和。例如:字符串是:‘hello90abc 78sjh12.5’ 结果是90+78+12.5 = 180.5
str1 = 'hello90abc -78sjh12.5'
re_str=r'-?\d+\.?\d*'
all_nums = findall(re_str, str1)
print(all_nums)
sum1 = 0
for item in all_nums:
sum1 += float(item)
print(sum1)
3. 验证输入的内容只能是汉字
value = '你好'
result = fullmatch(r'[\u4e00-\u9af5]+', value)
if not result:
print('不是汉字')
print(result)
4. 电话号码的验证 # 电话号码正则表达式
re_str = r'1[3-9][0-9]{9}'
result = fullmatch(re_str, '13984848441')
print(result)