python核心編程十五章練習

15-1

import re


pattern = re.compile('([bh][aui]t )+')
match = re.match(pattern, 'bat bit but hat hit hut ')
print match.group()

15-2

import re

pattern = re.compile('\w+\s\w+')
match = re.match(pattern, 'Sheldon Cooper')
print match.group()

15-3

import re


pattern = re.compile('\w+,\s[A-Z]')
match = re.match(pattern, 'Cooper, Sheldon')
print match.group()
15-4

import re


pattern = re.compile('[_a-zA-Z][\w|_]+')
match = re.match(pattern, '_15_4')
print match.group()

15-5

import re


pattern = re.compile('(\d+\s)([a-zA-Z]+\s)+')
match1 = re.match(pattern, '1180 Bordeaux Drive')
match2 = re.match(pattern, '3120 De la Cruz Boulevard')
print match1.group(), match2.group()
15-6
import re


pattern = re.compile('www\.\w+\.(com|edu|net)')
match1 = re.match(pattern, 'www.yahoo.com')
match2 = re.match(pattern, 'www.ucsc.edu')
match3 = re.match(pattern, 'www.china.net')

print match1.gr

15-7
import re


pattern = re.compile('(0x|-0x|\d|-\d)[\da-fA-F]+')
match1 = re.match(pattern, '0x152')
match2 = re.match(pattern, '-0x11')
match3 = re.match(pattern, '-11')
match4= re.match(pattern, '12')

print match1.group(), match2.group(), match3.group(), match4.group()

15-9

import re


pattern = re.compile('-?\d*\.(\d+([e|E]?[+-]?\d+)?)?')
match1 = re.match(pattern, '11.1')
match2 = re.match(pattern, '11.')
match3 = re.match(pattern, '-11.')
match4 = re.match(pattern, '-11.2e1.1')
print match1.group(), match2.group(), match3.group(), match4.group()

15-10

import re


pattern = re.compile('(?P<num>-?\d*(\.)?(\d+([e|E]?[+-]?\d+)?)?)+(?P=num)[j|J]')
match = re.match(pattern, '1+1j')

print match.group()
15-11

import re 


pattern = re.compile('\w+@\w+\.(com|edu|org)')

match = re.match(pattern, '[email protected]')

print match.group()

15-12

import re 


pattern = re.compile('(http|ftp)(s)?://(www\.)?.+\.(net|com|org|cn).+')

match = re.match(pattern, 'http://baike.baidu.com/view/245485.htm')
match2 = re.match(pattern, 'https://www.bing.com/search?q=url&qs=n&form=QBRE&pq=url&sc=8-3&sp=-1&sk=&cvid=38B907362835475DB075C9C5EBB1FEC1')

print match.group(), match2.group()
15-13

import re


pattern = re.compile("<type\s'(\w+)'>")

ret = type(.34)
match = re.match(pattern, str(ret))

print match.group(1)
15-14

import re

pattern = re.compile('^(0?[1-9])$|^(10|11|12)$')

for i in range(20):
    match = re.match(pattern, str(i))
    if match is not None:
        print match.group()

15-15
import re


pattern = re.compile('(\d{4}-\d{6}-\d{5})|(\d{4}-\d{4}-\d{4})')

match = re.match(pattern, '1111-1111-1111-1411')

print match.group()

15-16
#!/usr/bin/env python

from random import randint, choice
from string import lowercase
from sys import maxint
from time import ctime

doms = ('com', 'edu', 'net', 'org', 'gov')
f = open('redata.txt', 'a')
for i in range(randint(5, 10)):
    dtint = randint(0, maxint-1)  # pick date
    dtstr = ctime(dtint)        # date string

    shorter = randint(4, 7)        # login shorter
    em = ''
    for j in range(shorter):        # generate login
        em += choice(lowercase)

    longer = randint(shorter, 12)  # domain longer
    dn = ''
    for j in range(longer):         # create domain
        dn += choice(lowercase)

    f.write('%s::%s@%s.%s::%d-%d-%d\n' % (dtstr, em,
        dn, choice(doms), dtint, shorter, longer))

f.close()

15-17
import re


pattern = re.compile('(\w{3}).+')
mon = tue = wed = thu = fri = sat = sun = 0
with open('redata.txt') as f:
    for eachline in f:
        match = re.match(pattern, eachline)
        m = match.group(1)
        if m == 'Mon': mon += 1
        if m == 'Tue': tue += 1
        if m == 'Wed': wed += 1
        if m == 'Thu': thu += 1
        if m == 'Fri': fri += 1
        if m == 'Sat': sat += 1
        if m == 'Sun': sun += 1

print mon, tue, wed, thu, fri, sat, sun

15-18
import re
from time import strptime, mktime


pattern = re.compile('(.+)::.+::(\d+).+')

with open('redata.txt') as f:
    for eachline in f:
        match = re.match(pattern, eachline)
        if match is not None:
            t1 = mktime(strptime(match.group(1)))
            t2 = float(match.group(2))
            if t1 == t2:
                print eachline, 'ok' 
15-19

f = open('redata.txt', 'r')
time = []
for eachline in f:
    time.append(eachline.split('::')[0])
f.close()
print time

15-20

f = open('redata.txt', 'r')
email = []
for eachline in f:
    email.append(eachline.split('::')[1])
f.close()

print email

15-21
import re


pattern = re.compile('\w{3}\s(\w{3}).+')
f = open('redata.txt', 'r')
mon = []
for eachline in f:
    match = re.match(pattern, eachline)
    if match is not None:
        mon.append(match.group(1))
f.close()
print mon

15-22
import re


f = open('redata.txt','r')
year = []
for eachline in f:
    pattern = re.compile('.+?\s(\d{4}).+')
    match = re.match(pattern, eachline)
    year.append(match.group(1))
print year
f.close()
15-23
import re


pattern = re.compile('.+?(\d{2}:\d{2}:\d{2}).+')
f = open('redata.txt', 'r')
time = []
for eachline in f:
    match = re.match(pattern, eachline)
    time.append(match.group(1))

print time
f.close()
15-24
import re


pattern = re.compile('::(\w+)@(.+)::')
f = open('redata.txt', 'r')
email = []
for eachline in f:
    match = re.search(pattern, eachline)
    matchgroup = match.group(1) + match.group(2)
    email.append(matchgroup)

print email
f.close()
15-25
import re


pattern = re.compile('::(\w+)@(.+)::')
f = open('redata.txt', 'r')
login = []
domain = []
for eachline in f:
    match = re.search(pattern, eachline)
    login.append(match.group(1))
    domain.append(match.group(2))

print login
print domain
f.close()

15-26
import re


pattern = re.compile('::(.+)::')
sub = []
with open('redata2.txt', 'r') as f:
    for eachline in f:
        s = re.sub(pattern, '::[email protected]::', eachline)
        sub.append(s)

with open('redata2.txt', 'w') as f:
    f.write(''.join(sub))
15-27
import re


pattern = re.compile('\w{3}\s(\w{3})\s(\d\d).+?(\d{4}).+')

with open('redata.txt', 'r') as f:
    for eachline in f:
        match = re.match(pattern, eachline)
        if match is not None:
            print '%s %s, %s' % (match.group(1), match.group(2), match.group(3))
15-28
import re


pattern = re.compile('(\d{3}-)?\d{3}-\d{4}')
match = re.match(pattern, '551-1212')
match1 = re.match(pattern, '111-111-1111')
print match.group(), match1.group()
15-29
import re


pattern = re.compile('(\(?\d{3}\)?[-|\s])?\d{3}-\d{4}')
match = re.match(pattern, '551-1212')
match1 = re.match(pattern, '111-111-1111')
match2 = re.match(pattern, '(800) 555-1212')
print match.group(), match1.group(), match2.group()







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章