python核心编程2 第十五章 练习
15-1.识别下列字符串 :“bat ”、“bit ”、“but ”、“hat ”、“hit” 或 “hut ”
import re
from random import choice strtuple = ('bat', 'bit', 'but', 'hat', 'hit', 'hut')
patt = '[bh][aiu]t'
m = re.search(patt, choice(strtuple))
print(m.group())
15-2.匹配用一个空格分隔的任意一对单词 ,比如名和姓
import re
from random import choice strtuple = ('Bei Liu', 'Yu Guan', 'Fei Zhang')
patt = '[A-Za-z]+\s[A-Za-z]+'
m = re.search(patt, choice(strtuple))
print(m.group())
15-3.匹配用一个逗号和一个空格分开的一个单词和一个字母 。例如英文人名中的姓和名的首字母
import re
from random import choice strtuple = ('Bei, L', 'Yu, G', 'Fei, Z')
patt = '[A-Za-z]+,\s[A-Za-z]?'
m = re.search(patt, choice(strtuple))
print(m.group())
15-4.匹配所有合法的 Python 标识符
import re
from random import choice strtuple = ('Abc_123', '123abc', '_abc123_', 'abcDef')
patt = '[A-Za-z_](\w+|_)*'
elem = choice(strtuple)
m = re.match(patt, elem)
if m is not None:
print(m.group())
else:
print('Mismatch:', elem)
15-5.请根据你 (读者〉 本地关于地址的格式写法匹配一个街道地址 (写出的正则表达式要尽可能通
用以匹配任意数目 的表示街道名字的单词 ,包括类型指示〉 。比如,美国的街道地址使用这样 的格式:1180 Bordeaux Drive 。
使你写的正则表达式尽可能通用,要求能够匹配多个单词的街道名字 ,如:3120 De la Cruz Boulevard 。
import re
from random import choice strtuple = ('1180 Bordeaux Drive', '3120 De la Cruz Boulevard')
patt = '\d{4}\s\w+\s\w+(\s\w+\s\w+)?'
m = re.match(patt, choice(strtuple)).group()
print(m)
15-6.匹配简单的以 “WWW. ” 开头 ,以 “.com” 作结尾的 Web 域名 ,例如 :www.yahoo.com.
附加题:使你写的正则表达式还支持其他顶级域名如 .edu 、.net 等比如 www.ucsc.edu 。
import re
from random import choice strtuple = ('http://www.cnki.net', 'http://www.neea.edu.cn', 'http://www.pbccrc.org.cn',
'https://gab.122.gov.cn', 'www.cnblogs.com')
patt = '(http(s)?://)?\w{3}\.\w+\.\w{3}(\.cn)?'
m = re.match(patt, choice(strtuple)).group()
print(m)
15-7.匹配全体Python整型的字符串表示形式的集合
import re
from random import choice strtuple = ('', '', '')
patt = '\d+'
m = re.match(patt, choice(strtuple)).group()
print(m)
15-8.匹配全体Python长整型的字符串表示形式的集合
import re
from random import choice strtuple = ('11111L', '222222222222222222L', '33L')
patt = '\d+L'
m = re.match(patt, choice(strtuple)).group()
print(m)
15-9.匹配全体Python浮点型的字符串表示形式的集合
import re
from random import choice strtuple = ('1.01', '2.0', '3.141592654')
patt = '\d+\.\d+'
m = re.match(patt, choice(strtuple)).group()
print(m)
15-10.匹配全体Python复数的字符串表示形式的集合
import re
from random import choice strtuple = ('1+2.3j', '2.1+5j', '5.3+0.5j')
patt = '((\d+)?(\d+\.\d+)?\+(\d+)?(\d+\.\d+)?)j'
m = re.match(patt, choice(strtuple)).group()
print(m)
15-11.匹配所有合法的电子邮件地址 (先写出一个限制比较宽松的正则表达式 ,然后尽可能加强限制条件 ,但要保证功能的正确性) 。
import re
from random import choice strtuple = ('abc@163.com','123@sina.com','456@sina.cn', 'abc@abc.abc.com')
patt = '\w+@\w+(\.\w+)?\.\w{1,3}'
m = re.match(patt, choice(strtuple)).group()
print(m)
15-12.匹配所有合法的 Web 网站地址 (URL)(先写出一个限制比较宽松的正则表达式 ,然后尽可能加强限制条件 ,但要保证功能的正确性) 。
import re
from random import choice strtuple = ('http://www.cnki.net', 'http://www.neea.edu.cn', 'http://www.pbccrc.org.cn',
'https://gab.122.gov.cn', 'www.cnblogs.com')
patt = '(http(s)?://)?\w{3}\.\w+\.\w{3}(\.cn)?'
m = re.match(patt, choice(strtuple)).group()
print(m)
15-13.type(). type()内建函数返回一个对象类型 ,此对象显示为 Python的字符串形式 ,如下所示 :
>>> type ( 0)
<type ’ int ’>
>>> type ( .34 )
<type ’ f loat ’>
>>> type (dir)
<type ’ builtin_f unction_or_method ’>
请写一个正则表达式,能从这个字符串中提取出类型的名字。你的函数能实现以下功能:如果 字符串 “<type 'int’>“ 做输入,会返回类型 “int” (返回其他类型也同理 ,如,返回类型 ‘float’ , 'builtin_function_or_method’ 等)。提示:正确的结果保存在类和某些内建类型__name__属性里
import re
from random import choice strtuple = (0, 0.34, dir)
typestr = type(choice(strtuple))
patt = "<\w+ '(\w+)'>"
m = re.match(patt, str(typestr))
print(m.group(1))
15-14.正则表达式 。在 15.2 小节里 ,我们给出一个匹配由一位或两位数字代表一月到九月的字符串
形式 (“ 0?[1-9]")。请写出一个正则表达式表示标准日历上其他的三个月(十月、十一月、十二月)。
import re
from random import choice btuple = (10, 11, 12)
patt = '1+[0-2]'
m = re.match(patt, str(choice(btuple)))
print(m.group())
15-15.正则表达式 。在 15.2 小节里,我们给出一个匹配信用卡卡号的模式:("[0-9]{15, 16}")。但这个模式不允许用连字符号分割信用卡卡号中的数字 。请写出一个允许使用连字符的正则表达式,但要求连字符必须出现在正确的位置 。例如,15位的信用卡卡号的格式是 4-6-5 ,表示四个数字,一个连字符 ,后面接 6 个数字、1 个连字符 ,最后是 5 个数字 。16 位的信用卡卡号的格式是 4-4-4-4 ,数位不足时 ,添 0 补位。附加题:有一个用于确定某个信用卡卡号是否 合法的算法 。请写一段代码 ,它不但能识别格式正确的信用卡卡号 ,还能验证它的有效性 。
import re
from random import choice strtuple = ('1234-123456-12345', '1234-4321-1234-4321')
patt = '\d{4}-\d{6}-\d{5}|\d{4}-\d{4}-\d{4}-\d{4}'
m = re.match(patt, choice(strtuple))
print(m.group())
15-16.修改脚本gendata.py的代码,使数据直接写入文件redata.txt中,而不是输出到屏幕上。
from random import randint, choice
from string import ascii_lowercase
from sys import maxsize
from time import ctime domes = ('com', 'edu', 'net', 'org', 'gov') for i in range(randint(5, 10)):
dtint = randint(0, maxsize-1)
dtstr = ctime(dtint) shorter = randint(4, 7)
em = ''
for j in range(shorter):
em += choice(ascii_lowercase) longer = randint(shorter, 12)
dn = ''
for j in range(longer):
dn += choice(ascii_lowercase) data = '%s::%s@%s.%s::%d-%d-%d' % (dtstr, em, dn, choice(domes), dtint, shorter, longer)
with open('redata.txt', 'a') as f:
f.write(data + '\n')
15-17.统计生成的 redata.txt 文件中 ,星期中的每一天出现的次数 (或统计各月份出现的次数〉
import re
days = {'Mon': 0, 'Tue': 0, 'Wed': 0, 'Thu': 0, 'Fri': 0, 'Sat': 0, 'Sun': 0}
months = {'Jan': 0, 'Feb': 0, 'Mar': 0, 'Apr': 0, 'May': 0, 'Jun': 0, 'Jul': 0,
'Aug': 0, 'Sep': 0, 'Oct': 0, 'Nov': 0, 'Dec': 0}
patt = '(\w{3})\s(\w{3})'
f = open('redata.txt')
for line in f:
m = re.match(patt, line.strip())
if m.group(1) in days:
days[m.group(1)] += 1
if m.group(2) in months:
months[m.group(2)] += 1
f.close()
for key in days:
print('%s-%d' % (key, days[key]), end=' ')
print()
for key in months:
print('%s-%d' % (key, months[key]), end=' ')
15-18.通过检查每个输出行中整型字段部分的第一个整型是否和该行开头的时间戳相匹配来验证redata.txt 中的数据是否完好。
import re
patt = '\d+\s\d+:\d+:\d+'
f = open('redata.txt')
for line in f:
lines = line.strip()
m = re.search(patt, lines)
if m is not None:
print('Data is intact')
else:
print(lines)
f.close()
15-19.提取出每行中完整的时间戳字段
import re
patt = '\d+:\d+:\d+\s\d+'
f = open('redata.txt')
for line in f:
m = re.search(patt, line.strip())
print(m.group())
15-20.提取出每行中完整的电子邮件地址
import re
patt = '\w+@\w+\.\w{3}'
f = open('redata.txt')
for line in f:
m = re.search(patt, line.strip())
print(m.group())
15-21.只提取出时间戳字段中的月份。
import re
patt = '\w{3}\s(\w{3})'
f = open('redata.txt')
for line in f:
m = re.match(patt, line.strip())
print(m.group(1))
15-22.只提取出时间戳字段中的年份 。
import re
patt = '\d+:\d+:\d+\s(\d+)'
f = open('redata.txt')
for line in f:
m = re.search(patt, line.strip())
print(m.group(1))
15-23.只提取出时间戳字段中的值 (格式:HH:MM:SS )。
import re
patt = '\d+:\d+:\d+'
f = open('redata.txt')
for line in f:
m = re.search(patt, line.strip())
print(m.group())
15-24.只从电子邮件地址中提取出登录名和域名 (包括主域名和顶级域名 ,二者连在一起〉
import re
patt = '(\w+)@(\w+\.\w{3})'
f = open('redata.txt')
for line in f:
m = re.search(patt, line.strip())
print('login:',m.group(1), '\ndomain:', m.group(2))
15-25.只从电子邮件地址中提取出登录名和域名 (包括主域名和顶级域名 ,二者分别提取〉
import re
patt = '(\w+)@(\w+)\.(\w{3})'
f = open('redata.txt')
for line in f:
m = re.search(patt, line.strip())
print('login:',m.group(1), '\nprimary domain:', m.group(2),
'\ntop level domain:', m.group(3))
15-26.将每行中的电子邮件地址替换为你自己的电子邮件地址 。
import re
patt = '\w+@\w+\.\w{3}'
mail = 'abc@abc.com'
f = open('redata.txt')
for line in f:
m = re.sub(patt, mail, line.strip())
with open('newredata.txt', 'a') as newfile:
newfile.write(m + '\n')
f.close()
15-27.提取出时间戳中的月 、日、年,并按照格式 “月、日、年” 显示出来 ,且每行仅遍历一次 。
import re
patt = '(\w{3})\s\s?(\d+)\s\d+:\d+:\d+\s(\d+)'
f = open('redata.txt')
for line in f:
m = re.search(patt, line.strip())
print(m.group(1), m.group(2), m.group(3))
15-28.区号(第一组的三个数字和它后面的连字符〉是可选的 ,即,你写的正则表达式对 800-555-1212
和 555-1212 都可以匹配 。
import re
patt = '(\d+-)?\d+-\d+'
f = open('redata.txt')
for line in f:
m = re.search(patt, line.strip())
print(m.group())
15-29.区号中可以包含圆括号或是连字符 ,而且它们是可选的 ,就是说你写的正则表达式可以匹配
800-555-1212 、555-1212 或 ( 800) 555-12120
import re
patt = '(\(\d+\))?(\d+-)?\d+-\d+'
f = open('redata.txt')
for line in f:
m = re.search(patt, line.strip())
print(m.group())
python核心编程2 第十五章 练习的更多相关文章
- python核心编程2 第十四章 练习
14-3.执行环境.创建运行其他Python脚本的脚本. if __name__ == '__main__': with open('test.py') as f: exec(f.read()) 14 ...
- Python 核心编程 课后习题 第五章
2. 操作符. (a) 写一个函数, 计算并返回两个数的乘积. (b) 写一段代码调用这个函数, 并显示它的结果. def multi(a,b): return a * b result = mult ...
- python核心编程2 第十二章 练习
12–5. 使用 __import__().(a) 使用 __import__ 把一个模块导入到你的名称空间. 你最后使用了什么样的语法? (b) 和上边相同, 使用 __import__() 从指定 ...
- Python核心编程第三版第二章学习笔记
第二章 网络编程 1.学习笔记 2.课后习题 答案是按照自己理解和查阅资料来的,不保证正确性.如由错误欢迎指出,谢谢 1. 套接字:A network socket is an endpoint of ...
- 流畅python学习笔记:第十五章:上下文管理器
在开始本章之前,我们首先来谈谈try-excep..final模块.在Python中,进行异常保护的最多就是用try..except..final.首先来看下下面的代码.进行一个简单的除法运算.为了防 ...
- Python核心编程课后习题-第六章
1. 字符串, string模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? str1 = 'abcdefghijklmnopqrstuv' print st ...
- 《windows核心编程系列》十五谈谈windows线程栈
谈谈windows线程栈. 当系统创建线程时会为线程预订一块地址空间区域,注意仅仅是预订.默认情况下预定的这块区域的大小是1MB,虽然预订这么多,但是系统并不会给全部区域调拨物理存储器.默认情况下,仅 ...
- 进击的Python【第十五章】:Web前端基础之DOM
进击的Python[第十五章]:Web前端基础之DOM 简介:文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示 ...
- 第十五章、python中的进程操作-开启多进程
目录 第十五章.python中的进程操作-开启多进程 一.multprocess模块 二.multprocess.process模块 三.Process()对象方法介绍 四.Process()对象属性 ...
随机推荐
- 3D开源推荐:3DWebExplorer
开源网址:https://github.com/irconde/3DWebExplorer 介绍:演示如何内嵌Google Earth 插件,开发面向公众的3D旅游展示平台
- svn图标显示不正常,文件夹显示但文件不显示svn图标
svn图标显示不正常,文件夹显示但文件不显示svn图标 这个问题的引发是自己造成的,使用myEclipse时progress会卡在 refresh svn status cache (0%)这里, ...
- SharePoint 2010配置PDF文件全文检索
一.安装Adobe PDF 64 bit IFilter version 9合Adobe Reader 9下载地址: http://www.adobe.com/support/downloads/de ...
- Spring中<context:annotation-config/>的作用
spring中<context:annotation-config/>配置的作用,现记录如下: <context:annotation-config/>的作用是向Spring容 ...
- ul自适应li问题
内容提要: li浮动时ul高度为0,解决ul自适应高度的几种方法 在网页设计中,常常需要对li标签做浮动效果,但是在不同浏览器中会遇到兼容性问题,比如IE中会出现ul高度为0的情况,是效果不能达到预期 ...
- March 31 2017 Week 13 Friday
Sometimes, you think the sky is falling down, actually, that is just because you stand slanting. 有时候 ...
- March 13 2017 Week 11 Monday
A warm smile is the universal language of kindness. 温暖的笑容是善意的通用语. Face comes from the heart. Just sm ...
- 原生JavaScript实现JSON合并(递归深度合并)
// 遇到相同元素级属性,以(minor)为准 // 不返还新Object,而是main改变 function mergeJSON(minor, main) { for(var key in mino ...
- IOS Xib使用
- 生理周期,POJ(1006)
题目链接:http://poj.org/problem?id=1006 解题报告: 1.枚举天数的时候可以根据前面的结果直接跳过一些错误的答案. ///三个周期是23,28,33, #include ...