Python核心编程——Chapter15

正则表达式在脚本语言里是最重要的一部分,这部分的题目真的不容怠慢。

开始这部分的题目的解答!

15.1识别下列字符串:bat,bit,but,hat,hit和hut。

>>> import re
>>> pattern='[bh][aiu]t'
>>> word='batsasasasa'
>>> m=re.search(pattern,word)
>>> if m is not None:
...     m.group()
... 
'bat'

15.2.匹配用一个空格分隔的任意一对单词,比如名和性。

pattern='[A-Za-z][a-z]+ [A-Za-z][a-z]'

15.3.匹配用一个逗号和一个空格分开的一个单词和一个字母。

>>> import re
>>> pattern='([A-Z].)+ ?[A-Z][a-z]+'
>>> s1='J.R. Smith'
>>> s2='J.R.Smith'
>>> s3='T. Ford'
>>> re.match(pattern,s1).group()
'J.R. Smith'
>>> re.match(pattern,s2).group()
'J.R.Smith'
>>> re.match(pattern,s3).group()
'T. Ford'

15.4.匹配所有合法的Python标识符

所谓合法的Python标识符:首字母只能是下划线或字母,然后之后的字符可以是字母,数字或下划线。

>>> pattern='[a-zA-Z_][w_]+'

15.5.匹配美国的街道名字地址,如:1180 Bordeaux Drive ,3120 De la Cruz Boulevard。

>>> patter='d+ [A-Za-z ]+'

15.6.匹配以“www."开头,以”.com"作结尾的Web域名。

>>> pattern='w{3}[.w]+.com'

附加题:支持其他顶级域名:

>>> pattern='w{3}[.w]+'

 15.7.匹配全体Python整型的字符串表示形式的集合。

>>> pattern='d+[Ll]?'

15.8.匹配全体Python长整型字符串表示形式的集合。

>>> pattern='d+[Ll]'

15.9.匹配全体Python浮点型的字符串表示形式的集合。

>>> pattern='d+.d+'

15.10.匹配全体Python复数的字符串表示形式的集合。

>>> pattern='d+.?d++d+.?d+j'

15.11.匹配所有合法的电子邮件地址

>>> pattern='w+@[w.]+'

15.13.往type()提取类型的名字

>>> pattern=''
>>> re.match(pattern,"<type 'int'>").group()
"<type 'int'>"
>>> re.match(pattern,"<type 'int'>").group(1)
'int'

 15.16.将gendata.py的内容输出到文件当中。

#!/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')
g = open('/home/dzhwen/456.txt','a+')

for i in range(randint(5,10)):
    dtint = randint(0,maxint-1)
    dtstr = ctime(dtint)
    shorter = randint(4,7)

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

    word=dtstr+'::'+em+'@'+dn+'.'+choice(doms)+'::'+str(dtint)+'-'+str(shorter)+'-'+str(longer)+'
'
    g.write(word)

15.19.提取出每行中完整的时间戳字段。

#!/usr/bin/env python

import re

f = open('/home/dzhwen/456.txt','r')

pattern = '(.+?)::.+'

for eachLine in f:
    m = re.match(pattern,eachLine)
    print m.group(1)

15.20.提取出每行中完整的电子邮件地址。

#!/usr/bin/env python

import re

f = open('/home/dzhwen/456.txt','r')

pattern = '.+::(w+@w+.w+)::.+'

for eachLine in f:
    m = re.match(pattern,eachLine)
    print m.group(1)

15.21.只提取时间戳字段中的月份。

#!/usr/bin/env python

import re

f = open('/home/dzhwen/456.txt','r')

pattern = 'w{3} (w{3}).+'

for eachLine in f:
    m = re.match(pattern,eachLine)
    print m.group(1)

15.22.只提取时间戳字段中的年份。 

#!/usr/bin/env python

import re

f = open('/home/dzhwen/456.txt','r')

pattern = '.+?(d{4}).+'

for eachLine in f:
    m = re.match(pattern,eachLine)
    print m.group(1)

 15.23.只提取出时间戳字段中的时间值。

#!/usr/bin/env python

import re

f = open('/home/dzhwen/456.txt','r')

pattern = '.+(d{2}:d{2}:d{2}).+'

for eachLine in f:
    m = re.match(pattern,eachLine)
    print m.group(1)
    

 15.25.只从电子邮件地址中提取出登录名和域名。(二者分别提取)

#!/usr/bin/env python

import re

f = open('/home/dzhwen/456.txt','r')

pattern = '.+::(w+)?@(.+)?::.+'

for eachLine in f:
    m = re.match(pattern,eachLine)
    print m.group(1),m.group(2)

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

#!/usr/bin/env python
#-*-coding:utf-8-*-

import re

f = open('/home/dzhwen/456.txt','r')

pattern = '.+::(.+)?::.+'

for eachLine in f:
    m = re.match(pattern,eachLine)
    address = raw_input('请输入你自己的电子邮件:')
    print re.subn(m.group(1),address,eachLine)

有趣的题目基本只有这些,请多多指教!

原文地址:https://www.cnblogs.com/sysu-blackbear/p/3604021.html