15-1.识别下列字符串 :“bat ”、“bit ”、“but ”、“hat ”、“hit” 或 “hut ”

1 importre2 from random importchoice3

4

5 strtuple = ('bat', 'bit', 'but', 'hat', 'hit', 'hut')6 patt = '[bh][aiu]t'

7 m =re.search(patt, choice(strtuple))8 print(m.group())

15-2.匹配用一个空格分隔的任意一对单词 ,比如名和姓

1 importre2 from random importchoice3

4

5 strtuple = ('Bei Liu', 'Yu Guan', 'Fei Zhang')6 patt = '[A-Za-z]+\s[A-Za-z]+'

7 m =re.search(patt, choice(strtuple))8 print(m.group())

15-3.匹配用一个逗号和一个空格分开的一个单词和一个字母 。例如英文人名中的姓和名的首字母

1 importre2 from random importchoice3

4

5 strtuple = ('Bei, L', 'Yu, G', 'Fei, Z')6 patt = '[A-Za-z]+,\s[A-Za-z]?'

7 m =re.search(patt, choice(strtuple))8 print(m.group())

15-4.匹配所有合法的 Python 标识符

1 importre2 from random importchoice3

4

5 strtuple = ('Abc_123', '123abc', '_abc123_', 'abcDef')6 patt = '[A-Za-z_](\w+|_)*'

7 elem =choice(strtuple)8 m =re.match(patt, elem)9 if m is notNone:10 print(m.group())11 else:12 print('Mismatch:', elem)

15-5.请根据你 (读者〉 本地关于地址的格式写法匹配一个街道地址 (写出的正则表达式要尽可能通

用以匹配任意数目 的表示街道名字的单词 ,包括类型指示〉 。比如,美国的街道地址使用这样 的格式:1180 Bordeaux Drive 。

使你写的正则表达式尽可能通用,要求能够匹配多个单词的街道名字 ,如:3120 De la Cruz Boulevard 。

1 importre2 from random importchoice3

4 strtuple = ('1180 Bordeaux Drive', '3120 De la Cruz Boulevard')5 patt = '\d{4}\s\w+\s\w+(\s\w+\s\w+)?'

6 m =re.match(patt, choice(strtuple)).group()7 print(m)

15-6.匹配简单的以 “WWW. ” 开头 ,以 “.com” 作结尾的 Web 域名 ,例如 :www.yahoo.com.

附加题:使你写的正则表达式还支持其他顶级域名如 .edu 、.net 等比如 www.ucsc.edu 。

1 importre2 from random importchoice3

4 strtuple = ('http://www.cnki.net', 'http://www.neea.edu.cn', 'http://www.pbccrc.org.cn',5 'https://gab.122.gov.cn', 'www.cnblogs.com')6 patt = '(http(s)?://)?\w{3}\.\w+\.\w{3}(\.cn)?'

7 m =re.match(patt, choice(strtuple)).group()8 print(m)

15-7.匹配全体Python整型的字符串表示形式的集合

1 importre2 from random importchoice3

4 strtuple = ('123', '456', '789')5 patt = '\d+'

6 m =re.match(patt, choice(strtuple)).group()7 print(m)

15-8.匹配全体Python长整型的字符串表示形式的集合

1 importre2 from random importchoice3

4 strtuple = ('11111L', '222222222222222222L', '33L')5 patt = '\d+L'

6 m =re.match(patt, choice(strtuple)).group()7 print(m)

15-9.匹配全体Python浮点型的字符串表示形式的集合

1 importre2 from random importchoice3

4 strtuple = ('1.01', '2.0', '3.141592654')5 patt = '\d+\.\d+'

6 m =re.match(patt, choice(strtuple)).group()7 print(m)

15-10.匹配全体Python复数的字符串表示形式的集合

1 importre2 from random importchoice3

4 strtuple = ('1+2.3j', '2.1+5j', '5.3+0.5j')5 patt = '((\d+)?(\d+\.\d+)?\+(\d+)?(\d+\.\d+)?)j'

6 m =re.match(patt, choice(strtuple)).group()7 print(m)

15-11.匹配所有合法的电子邮件地址 (先写出一个限制比较宽松的正则表达式 ,然后尽可能加强限制条件 ,但要保证功能的正确性) 。

1 importre2 from random importchoice3

4 strtuple = ('abc@163.com','123@sina.com','456@sina.cn', 'abc@abc.abc.com')5 patt = '\w+@\w+(\.\w+)?\.\w{1,3}'

6 m =re.match(patt, choice(strtuple)).group()7 print(m)

15-12.匹配所有合法的 Web  网站地址 (URL)(先写出一个限制比较宽松的正则表达式 ,然后尽可能加强限制条件 ,但要保证功能的正确性) 。

1 importre2 from random importchoice3

4 strtuple = ('http://www.cnki.net', 'http://www.neea.edu.cn', 'http://www.pbccrc.org.cn',5 'https://gab.122.gov.cn', 'www.cnblogs.com')6 patt = '(http(s)?://)?\w{3}\.\w+\.\w{3}(\.cn)?'

7 m =re.match(patt, choice(strtuple)).group()8 print(m)

15-13.type(). type()内建函数返回一个对象类型 ,此对象显示为 Python的字符串形式 ,如下所示 :

>>> type ( 0)

>>> type ( .34 )

>>> type (dir)

请写一个正则表达式,能从这个字符串中提取出类型的名字。你的函数能实现以下功能:如果 字符串 “<type 'int’>“ 做输入,会返回类型 “int” (返回其他类型也同理 ,如,返回类型 ‘float’ , 'builtin_function_or_method’ 等)。提示:正确的结果保存在类和某些内建类型__name__属性里

1 importre2 from random importchoice3

4 strtuple = (0, 0.34, dir)5 typestr =type(choice(strtuple))6 patt = ""

7 m = re.match(patt, str(typestr))

8 print(m.group(1))

15-14.正则表达式 。在 15.2 小节里 ,我们给出一个匹配由一位或两位数字代表一月到九月的字符串

形式 (“ 0?[1-9]")。请写出一个正则表达式表示标准日历上其他的三个月(十月、十一月、十二月)。

1 importre2 from random importchoice3

4 btuple = (10, 11, 12)5 patt = '1+[0-2]'

6 m =re.match(patt, str(choice(btuple)))7 print(m.group())

15-15.正则表达式 。在 15.2 小节里,我们给出一个匹配信用卡卡号的模式:("[0-9]{15, 16}")。但这个模式不允许用连字符号分割信用卡卡号中的数字 。请写出一个允许使用连字符的正则表达式,但要求连字符必须出现在正确的位置 。例如,15位的信用卡卡号的格式是 4-6-5 ,表示四个数字,一个连字符 ,后面接 6 个数字、1 个连字符 ,最后是 5 个数字 。16 位的信用卡卡号的格式是 4-4-4-4 ,数位不足时 ,添 0 补位。附加题:有一个用于确定某个信用卡卡号是否 合法的算法 。请写一段代码 ,它不但能识别格式正确的信用卡卡号 ,还能验证它的有效性 。

1 importre2 from random importchoice3

4 strtuple = ('1234-123456-12345', '1234-4321-1234-4321')5 patt = '\d{4}-\d{6}-\d{5}|\d{4}-\d{4}-\d{4}-\d{4}'

6 m =re.match(patt, choice(strtuple))7 print(m.group())

15-16.修改脚本gendata.py的代码,使数据直接写入文件redata.txt中,而不是输出到屏幕上。

1 from random importrandint, choice2 from string importascii_lowercase3 from sys importmaxsize4 from time importctime5

6 domes = ('com', 'edu', 'net', 'org', 'gov')7

8 for i in range(randint(5, 10)):9 dtint = randint(0, maxsize-1)10 dtstr =ctime(dtint)11

12 shorter = randint(4, 7)13 em = ''

14 for j inrange(shorter):15 em +=choice(ascii_lowercase)16

17 longer = randint(shorter, 12)18 dn = ''

19 for j inrange(longer):20 dn +=choice(ascii_lowercase)21

22 data = '%s::%s@%s.%s::%d-%d-%d' %(dtstr, em, dn, choice(domes), dtint, shorter, longer)23 with open('redata.txt', 'a') as f:24 f.write(data + '\n')

15-17.统计生成的 redata.txt 文件中 ,星期中的每一天出现的次数 (或统计各月份出现的次数〉

1 importre2

3 days = {'Mon': 0, 'Tue': 0, 'Wed': 0, 'Thu': 0, 'Fri': 0, 'Sat': 0, 'Sun': 0}4

5 months = {'Jan': 0, 'Feb': 0, 'Mar': 0, 'Apr': 0, 'May': 0, 'Jun': 0, 'Jul': 0,6 'Aug': 0, 'Sep': 0, 'Oct': 0, 'Nov': 0, 'Dec': 0}7

8 patt = '(\w{3})\s(\w{3})'

9

10 f = open('redata.txt')11 for line inf:12 m =re.match(patt, line.strip())13 if m.group(1) indays:14 days[m.group(1)] += 1

15 if m.group(2) inmonths:16 months[m.group(2)] += 1

17 f.close()18

19 for key indays:20 print('%s-%d' % (key, days[key]), end=' ')21 print()22 for key inmonths:23 print('%s-%d' % (key, months[key]), end=' ')

15-18.通过检查每个输出行中整型字段部分的第一个整型是否和该行开头的时间戳相匹配来验证redata.txt  中的数据是否完好。

1 importre2

3 patt = '\d+\s\d+:\d+:\d+'

4

5 f = open('redata.txt')6 for line inf:7 lines =line.strip()8 m =re.search(patt, lines)9 if m is notNone:10 print('Data is intact')11 else:12 print(lines)13 f.close()

15-19.提取出每行中完整的时间戳字段

1 importre2

3 patt = '\d+:\d+:\d+\s\d+'

4

5 f = open('redata.txt')6 for line inf:7 m =re.search(patt, line.strip())8 print(m.group())

15-20.提取出每行中完整的电子邮件地址

1 importre2

3 patt = '\w+@\w+\.\w{3}'

4

5 f = open('redata.txt')6 for line inf:7 m =re.search(patt, line.strip())8 print(m.group())

15-21.只提取出时间戳字段中的月份。

1 importre2

3 patt = '\w{3}\s(\w{3})'

4

5 f = open('redata.txt')6 for line inf:7 m =re.match(patt, line.strip())8 print(m.group(1))

15-22.只提取出时间戳字段中的年份 。

1 importre2

3 patt = '\d+:\d+:\d+\s(\d+)'

4

5 f = open('redata.txt')6 for line inf:7 m =re.search(patt, line.strip())8 print(m.group(1))

15-23.只提取出时间戳字段中的值 (格式:HH:MM:SS )。

1 importre2

3 patt = '\d+:\d+:\d+'

4

5 f = open('redata.txt')6 for line inf:7 m =re.search(patt, line.strip())8 print(m.group())

15-24.只从电子邮件地址中提取出登录名和域名   (包括主域名和顶级域名 ,二者连在一起〉

1 importre2

3 patt = '(\w+)@(\w+\.\w{3})'

4

5 f = open('redata.txt')6 for line inf:7 m =re.search(patt, line.strip())8 print('login:',m.group(1), '\ndomain:', m.group(2))

15-25.只从电子邮件地址中提取出登录名和域名   (包括主域名和顶级域名 ,二者分别提取〉

1 importre2

3 patt = '(\w+)@(\w+)\.(\w{3})'

4

5 f = open('redata.txt')6 for line inf:7 m =re.search(patt, line.strip())8 print('login:',m.group(1), '\nprimary domain:', m.group(2),9 '\ntop level domain:', m.group(3))

15-26.将每行中的电子邮件地址替换为你自己的电子邮件地址 。

1 importre2

3 patt = '\w+@\w+\.\w{3}'

4 mail = 'abc@abc.com'

5

6 f = open('redata.txt')7 for line inf:8 m =re.sub(patt, mail, line.strip())9 with open('newredata.txt', 'a') as newfile:10 newfile.write(m + '\n')11 f.close()

15-27.提取出时间戳中的月 、日、年,并按照格式 “月、日、年” 显示出来 ,且每行仅遍历一次 。

1 importre2

3 patt = '(\w{3})\s\s?(\d+)\s\d+:\d+:\d+\s(\d+)'

4

5 f = open('redata.txt')6 for line inf:7 m =re.search(patt, line.strip())8 print(m.group(1), m.group(2), m.group(3))

15-28.区号(第一组的三个数字和它后面的连字符〉是可选的 ,即,你写的正则表达式对 800-555-1212

和 555-1212 都可以匹配 。

1 importre2

3 patt = '(\d+-)?\d+-\d+'

4

5 f = open('redata.txt')6 for line inf:7 m =re.search(patt, line.strip())8 print(m.group())

15-29.区号中可以包含圆括号或是连字符 ,而且它们是可选的 ,就是说你写的正则表达式可以匹配

800-555-1212 、555-1212 或 ( 800) 555-12120

1 importre2

3 patt = '(\(\d+\))?(\d+-)?\d+-\d+'

4

5 f = open('redata.txt')6 for line inf:7 m =re.search(patt, line.strip())8 print(m.group())

python编程15章_python核心编程2 第十五章 练习相关推荐

  1. python课后题答案第一章_python核心编程课后习题解答第一章

    闲着没事,决定将<python核心编程>这本书的课后习题做一遍,以增加自己的编程能力. 1-1 将python安装到系统上 本人用的ubuntu系统,系统中自带了python,无需安装,本 ...

  2. python编程16章_Python核心编程——Chapter16

    好吧,在拜读完<Python网络编程基础>之后,回头再搞一搞16章的网络编程吧. Let's go! 16.4.修改书上示例的TCP和UDP客户端,使得服务器的名字不要在代码里写死,要允许 ...

  3. python程序练习题第三章_python核心编程-第三章-习题

    1.这是python的语言特性,python先创建对象,在给变量赋值时,不需要定义变量的名称和类型,它实际是用变量引用对象.变量类型在给变量赋值时自动声明 2.原因类似变量无须声明类型 3.pytho ...

  4. python教材答案第四章_python核心编程课后习题解答第四章

    4–1. Python 对象.与所有Python 对象有关的三个属性是什么?请简单的描述一下. type.ID.value..(身份.类型.值) type()接受一个对象作为参数,并返回它的类型 id ...

  5. python第五章课后编程题答案_Python核心编程-第五章课后习题

    5-1 整形 讲讲 Python 普通整型和长整型的区别 答:在2.7版本基本淡化了区别.真要区分的话,普通整型是32位或者64位,而长整型只与PC内存有关,很大就是了 5-2 运算符 (a) 写一个 ...

  6. python核心编程怎么做_Python核心编程:8个实践性建议

    前言 我们在用Python进行机器学习建模项目的时候,每个人都会有自己的一套项目文件管理的习惯,我自己也有一套方法,是自己曾经踩过的坑踩过的雷总结出来的,现在在这里分享一下给大家,因为很多伙伴是接触P ...

  7. 程序员编程艺术第三十四~三十五章:格子取数问题,完美洗牌算法

    第三十四~三十五章:格子取数,完美洗牌算法 作者:July.caopengcs.绿色夹克衫.致谢:西芹_new,陈利人, Peiyush Jain,白石,zinking. 时间:二零一三年八月二十三日 ...

  8. 程序员编程艺术第三十四 三十五章 格子取数问题,完美洗牌算法

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 第三十四 ...

  9. 第十五章 IO流(转换流 字符流 字符缓冲流 打印流)

    Java基础15 第十五章 IO流(转换流 字符流 字符缓冲流 打印流) 15.1 字符编码和字符集 15.1.1 字符编码 15.1.2 字符集 15.1.3 String类getBytes()方法 ...

最新文章

  1. cxGrid使用汇总4
  2. linux乌班图vi怎么使用,Ubuntu上vi(vim)编辑器使用教程
  3. 实现日、周、月排行统计 sql
  4. 面试题22. 链表中倒数第k个节点
  5. 【Java】@transient代表着什么
  6. 阅读笔记16-架构师推荐:提高90%开发效率的工具推荐
  7. python中gensim内没有summarization的问题
  8. 使用Microsoft Lookback网卡解决了断网情况下 Virtual Server 虚机和主机的网络连接
  9. maven中使用assembly打包
  10. Linux中action命令
  11. 腾讯云服务器修改和重置登录密码图文教程
  12. 【编解码:WebP协议】
  13. 俞敏洪一分钟励志演讲:
  14. MySQL的查询重写规则
  15. 纽约证交所代码表 zt
  16. sfc /scannow 提示 插入Windows XP SP3 CD 光盘
  17. 2022年天猫618满300减30红包怎么用?
  18. 天龙八部本服务器的注册码,天龙八部服务端配置! - 网游单机讨论 - 藏宝湾网游单机站 - Powered by Discuz!...
  19. 2016鄂教版小学信息技术初识计算机软件,鄂教版(2016)五年级全册信息技术 25.揭秘计算机工作世界--初识计算机工作原理 教案...
  20. 苹果内购噩梦条款3.1.1,知道这些小细节才不会被拒审

热门文章

  1. 分享20份小升初超实用简历模板,每套都有自己的风格,可选取适合孩子的
  2. 男朋友该对女生说的23句话(转)
  3. PySide2动态/静态加载UI及程序发布
  4. 提示计算机未安装flash,win10系统提示未安装Flash的解决方法
  5. Java 循环语句折纸小游戏
  6. 关于游戏性能优化的一些感想
  7. Q萌可爱,奇瑞QQ冰淇淋给年轻人爱的大礼物
  8. 【hibernate进阶】hibernate基本映射
  9. mysql语句按时间顺序排序_SQL语句怎么写?先按时间排序,再按姓名排序?
  10. 安卓逆向 -- Xposed模块编写