exercise

作者: 葡萄柚子茶 | 来源:发表于2018-09-11 21:01 被阅读0次
  1. 写一个正则表达式判断一个字符串是否是ip地址
    规则:一个ip地址由4个数字组成,每个数字之间用.连接。每个数字的大小是0-255

255.189.10.37 正确
256.189.89.9 错误

re_str=r'(2(5[0-5]{1}|[0-4]\d{1})|[0-1]?\d{1,2})(\.(2(5[0-5]{1}|[0-4]\d{1})|[0-1]?\d{1,2})){3}'
str1 = input('请输入ip地址:')
if fullmatch(re_str,str1):
     print('输入成功')
else:
     print('请输入正确的ip地址')
image.png
image.png
  1. 计算一个字符串中所有的数字的和
    例如:字符串是:‘hello90abc 78sjh12.5’ 结果是90+78+12.5 = 180.5
from re import fullmatch,findall
re_str = r'[1-9]\d*\.\d*|[1-9]\d+'
str1 = 'hello90abc 78sjh12.5'
result = findall(re_str,str1)
sum1 = 0
for num in result:
    num1 = float(num)
    sum1 += num1
print('这个字符串中的所有数字之和是:%.2f'%sum1)
#这个字符串中的所有数字之和是:180.50

  1. 验证输入的内容只能是汉字
re_str = r'^[\u4E00-\u9FFF]+$'
str1 = input('请输入:')
if fullmatch(re_str,str1):
    print('输入成功')
else:
    print('请输入正确的汉字')

image.png
  1. 电话号码的验证
re_str = r'[1][3,5,8][0-9]{9}'
str1 = input('请输入11位电话号码:')
if fullmatch(re_str,str1):
    print('输入成功')
else:
    print('请输入正确的电话号码')
image.png
image.png
  1. 简单的身份证号的验证
re_str = r'\d{6}(19\d{2}|200\d|201\d)(0[1-9]|1[0-2])(0[1-9]|[1-2]\d|3[0-1])\d{3}[\dXx]'
if re.fullmatch(re_str,'620104189306140437'):
    print('是合格的身份证号')
else:
    print('不是合格的身份证号')

image.png

二、不定项选择题

  1. 能够完全匹配字符串“(010)-62661617”和字符串“01062661617”的正则表达式包括( A B D)
 A. “\(?\d{3}\)?-?\d{8}”
 B. “[0-9()-]+”   
 C. “[0-9(-)]*\d*”   
 D. “[(]?\d*[)-]*\d*”
  1. 能够完全匹配字符串“c:\rapidminer\lib\plugs”的正则表达式包括( B C)
A. “c:\rapidminer\lib\plugs”  
B. “c:\\rapidminer\\lib\\plugs”  
C. “(?i)C:\\RapidMiner\\Lib\\Plugs”   ?i:将后面的内容的大写变成小写    
D. “(?s)C:\\RapidMiner\\Lib\\Plugs”   ?s:单行匹配  
  1. 能够完全匹配字符串“back”和“back-end”的正则表达式包括(A C D)
A. “\w{4}-\w{3}|\w{4}” match->back,back-end    fullmatch-> back,back-end  
B. “\w{4}|\w{4}-\w{3}” match-> back, back  fullmatch-> back,back-end  
C. “\S+-\S+|\S+”       
D. “\w*\b-\b\w*|\w*”  
  1. 能够完全匹配字符串“go go”和“kitty kitty”,但不能完全匹配“go kitty”的正则表达式包括( A D)
    :\1就是重复前面第一个()/组合里面的内容
    :\2就是重复前面第二个()/组合里面的内容
A. “\b(\w+)\b\s+\1\b”     
B. “\w{2,5}\s*\1”  
C. “(\S+) \s+\1”       
D. “(\S{2,5})\s{1,}\1”  
  1. 能够在字符串中匹配“aab”,而不能匹配“aaab”和“aaaab”的正则表达式包括(B C)
A. “a*?b”
B. “a{,2}b”   
C. “aa??b”  
D. “aaa??b”

相关文章

  • "Learn Python the Hard Way"学习笔记4

    Exercise 17 更多文件操作 执行结果: Exercise 18 名称,变量,代码,函数 Exercise...

  • 53: exercise

    exercise 07/22/2017 My topic is exercise. As we all know,...

  • success list-Nov. 7th, 2018

    1. Shanbei English Listening exercise 2. Dancing exercise...

  • success list-Nov. 9th, 2018

    1. Shanbei English Listening Exercise 2. Dancing Exercise...

  • exercise 0~exercise 5

    exercise 0 这一节练习主要讲了怎么安装python3和选择文件编辑器 exercise 1 这一节主要是...

  • JOS lab1

    Lab 1: Booting a PC PC Bootstrap Exercise 1. Exercise2 Pa...

  • "Learn Python the Hard Way"学习笔记5

    Exercise 24 更多练习 执行结果: Exercise 25 更多更多练习 在PowerShell里执行:...

  • exercise

    看介绍记忆力大师的培训班的视频 套路是:建立数字与实物的映射 知识 => 技能 => 价值 练习,兴趣,坚持

  • exercise

    1.声明一个电脑类: 属性:品牌、颜 、内存 法:打游戏、写代码、看视频a.创建电脑类的对象,然后通过对象点...

  • exercise

    提取data.json中的数据,将每条数据中的name、text、love和comment信息。并且保存到另外一个...

网友评论

      本文标题:exercise

      本文链接:https://www.haomeiwen.com/subject/vexpgftx.html