python核心编程2 第15章 练习

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

import re
from random import choicestrtuple = ('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 choicestrtuple = ('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 choicestrtuple = ('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 choicestrtuple = ('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 choicestrtuple = ('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 choicestrtuple = ('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 choicestrtuple = ('123', '456', '789')
patt = '\d+'
m = re.match(patt, choice(strtuple)).group()
print(m)

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

import re
from random import choicestrtuple = ('11111L', '222222222222222222L', '33L')
patt = '\d+L'
m = re.match(patt, choice(strtuple)).group()
print(m)

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

import re
from random import choicestrtuple = ('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 choicestrtuple = ('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 choicestrtuple = ('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 ( .34 )

>>> type (dir)

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

import re
from random import choicestrtuple = (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 choicebtuple = (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 choicestrtuple = ('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 ctimedomes = ('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 redays = {'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)] += 1if 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 repatt = '\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 repatt = '\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 repatt = '\w+@\w+\.\w{3}'f = open('redata.txt')
for line in f:m = re.search(patt, line.strip())print(m.group())

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

import repatt = '\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 repatt = '\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 repatt = '\d+:\d+:\d+'f = open('redata.txt')
for line in f:m = re.search(patt, line.strip())print(m.group())

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

import repatt = '(\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 repatt = '(\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 repatt = '\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 repatt = '(\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 repatt = '(\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 repatt = '(\(\d+\))?(\d+-)?\d+-\d+'f = open('redata.txt')
for line in f:m = re.search(patt, line.strip())print(m.group())

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部