本人最近在把《python核心编程》这本书刷一遍,方便日后自己查阅,同时也方便广大网友学习python,特将第一章的练习题答案分享于此(目前还停留在第一章),如有更好的方法或者答案有错,请私信我,非常感谢!
特别提醒:在运行代码之前,请先导入模块re import re
1-1
patt = '[b|h][a|i|u]t'
1-2
patt = '\w+ \w+'
1-3
patt = '\w+, \w+'
1-4
patt = '[a-zA-Z_]+[\w_]+'
1-5
patt = '\d{4}\s[\w\s]*'
1-6
patt = r'w{3}://w{3}.\w+(.(com|edu|net)/?)$'
1-7
`patt = r'[+-]?\d+'`
1-8
patt = r'[+-]?\d+[lL]'
1-9
patt = r'[+-]?\d*.\d*'
1-10
patt = r'\d*[+-]?\d*[i]?'
1-11
patt = r'[\w_]*@[\w_.]*.com$'
1-12
patt = r'http[s]?://w{3}.[\w_.]*.com$'
1-13
patt = "<type\s\'(\w+)\'>"
1-14
patt = '1[012]'
1-15
patt = r'\d{4}-\d+-\d+(?:-\d{4})?'
1-16
这里直接将随机生成的数据写入当前目录的redata.txt中
#用于正则表达式练习的数据生成器gendata.py
from random import randrange,choice
from string import ascii_lowercase as lc
from time import ctime
tlds = ('com','edu','net','org','gov')
f = open('redata.txt', 'w')
for i in range(randrange(5,11)):
dtint = randrange(2**32)#生成32位以下的正数:0~2^31-1
dtstr = ctime(dtint)#将生成的dtint转换成日期:日期从1970-1-1加上dtint秒数
llen = randrange(4, 8)
login = ''.join(choice(lc) for j in range(llen))#生成4-7位的用户名
dlen = randrange(llen,13)
dom = ''.join(choice(lc) for j in range(dlen))#生成llen-12位的服务器名
s = '%s::%s@%s.%s::%d-%d-%d\n'% (dtstr,login,dom,choice(tlds),dtint,llen,dlen)#将随机日期与邮箱输出
f.write(s)
f.close()
1-17
count = {'Mon': 0, 'Tue': 0, 'Wed': 0, 'Thu': 0, 'Fri': 0, 'Sat': 0, 'Sun': 0}
with open('redata.txt','r') as f:
while 1:
line = f.readline()
if not line:
break
patt = '(\w{3}).*'
day = re.match(patt, line).group(1)
count[day] = count[day] + 1
print(count)
1-18
from time import ctime
flag = 0
with open('redata.txt', 'r') as f:
while 1:
line = f.readline()
if not line:
break
patt = '([\w\s:]*)::([\w\s.@]*)::(\d+)-(?:[\d-]+)'
m = re.match(patt, line)
'''print(m)
print(line)'''
date = m.group(1)
date1 = ctime(int(m.group(3)))
if date == date1:
continue
else:
flag = 1
print('The file has been obstructed!')
if flag == 0:
print('No error!')
1-19
with open('redata.txt', 'r') as f:
while 1:
line = f.readline()
if not line:
break
patt = '(.*?)::([\w\s\.@]*)::(\d+)-(?:[\d-]+)'
m = re.match(patt, line)
print(m.group(3))
1-20
with open('redata.txt', 'r') as f:
while 1:
line = f.readline()
if not line:
break
patt = '(.*?)::([\w\s\.@]*)::(\d+)-(?:[\d-]+)'
m = re.match(patt, line)
print(m.group(2))
1-21
with open('redata.txt', 'r') as f:
while 1:
line = f.readline()
if not line:
break
patt = '\w{3}\s(\w{3}).*'
m = re.match(patt, line)
print(m.group(1))
1-22
with open('redata.txt', 'r') as f:
while 1:
line = f.readline()
if not line:
break
patt = '.*?(\d{4})::.*'
m = re.match(patt, line)
print(m.group(1))
1-23
#1-23
with open('redata.txt', 'r') as f:
while 1:
line = f.readline()
if not line:
break
patt = '.*?(\d{2}:\d{2}:\d{2}).*'
m = re.match(patt, line)
print(m.group(1))
1-24
#1-24
with open('redata.txt', 'r') as f:
while 1:
line = f.readline()
if not line:
break
patt = '.*?@([\w\.]+).*'
m = re.match(patt, line)
print(m.group(1))
1-25
#1-25
with open('redata.txt', 'r') as f:
while 1:
line = f.readline()
if not line:
break
patt = '.*?@(\w+)(\.\w*).*'
m = re.match(patt, line)
print('logname: %s, domainname: %s'%(m.group(1), m.group(2)))
1-26
#1-26
with open('redata.txt', 'r') as f:
while 1:
line = f.readline()
if not line:
break
patt = '\w*@[\w\.]+'
#m = re.search(patt, line)
m = re.sub(patt, '974106207@qq.com', line)
print(m)
1-27
#1-27
with open('redata.txt', 'r') as f:
while 1:
line = f.readline()
if not line:
break
patt = '(\w{3})\s(\w{3}).*?(\d{4}).*'
'''m = re.match(patt, line)
print('%s-%s-%s'%( m.group(2), m.group(1), m.group(3)))'''
print(re.sub(patt, r'\2-\1-\3', line))
1-28
#1-28
patt = '(\d{3}-)?\d{3}-\d{4}'
print(re.match(patt, '555-1212').group())
1-29
#1-29
patt = '(\d{3}-)?(\(\d{3}\)\s)?\d{3}-\d{4}'
re.match(patt, '555-1212').group()
以上代码均运行在python3环境,练习题1-30至1-32,目前还在探索中,若有会写者,希望能够分享代码!不尽感谢!