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

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

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)+'\n'
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)

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

Python核心编程——Chapter15的更多相关文章

  1. python核心编程第二版笔记

    python核心编程第二版笔记由网友提供:open168 python核心编程--笔记(很详细,建议收藏) 解释器options:1.1 –d   提供调试输出1.2 –O   生成优化的字节码(生成 ...

  2. python核心编程--笔记

    python核心编程--笔记 的解释器options: 1.1 –d   提供调试输出 1.2 –O   生成优化的字节码(生成.pyo文件) 1.3 –S   不导入site模块以在启动时查找pyt ...

  3. Python核心编程第二版(中文).pdf 目录整理

    python核心编程目录 Chapter1:欢迎来到python世界!-页码:7 1.1什么是python 1.2起源  :罗萨姆1989底创建python 1.3特点 1.3.1高级 1.3.2面向 ...

  4. python核心编程--笔记(不定时跟新)(转)

    的解释器options: 1.1 –d   提供调试输出 1.2 –O   生成优化的字节码(生成.pyo文件) 1.3 –S   不导入site模块以在启动时查找python路径 1.4 –v   ...

  5. python核心编程笔记(转)

    解释器options: 1.1 –d   提供调试输出 1.2 –O   生成优化的字节码(生成.pyo文件) 1.3 –S   不导入site模块以在启动时查找python路径 1.4 –v   冗 ...

  6. python核心编程(第二版)习题

    重新再看一遍python核心编程,把后面的习题都做一下.

  7. Python核心编程这本书的一些错误

    <Python核心编程第二版>这本书比<Python基础教程第二版修订版>详细很多,丰富了很多细节,虽然它是一本经典的入门书,但我发现还是存在一些明显的错误.在面向对象编程这一 ...

  8. Python核心编程-描述符

    python中,什么描述符.描述符就是实现了"__get__"."__set__"或"__delete__" 方法中至少一个的对象.什么是非 ...

  9. Python核心编程-闭包

    百度搜了一下闭包的概念:简而言之,闭包的作用就是在外部函数执行完并返回后,闭包使得收机制不会收回函数所占用的资源,因为内部函数的执行需要依赖外函数中的变量.这是对闭包作用的非常直白的描述,不专业也不严 ...

随机推荐

  1. [2017BUAA软工]个人项目心得体会:数独

    心得体会 回顾此次个人项目,感受比较复杂,最明显的一点是--累!代码编写.单元测试.代码覆盖.性能优化,环环相扣,有种从作业发布开始就一直在赶DDL的感觉,但是很充实,也学习到和体验了很多东西.最令人 ...

  2. 软工网络15团队作业8——Beta阶段敏捷冲刺(Day5)

    提供当天站立式会议照片一张 每个人的工作 1.讨论项目每个成员的昨天进展 赵铭: 进一步数据整理,写入数据库. 吴慧婷:主页面.查单词页面的改进.背单词界面改进. 陈敏: 单词学习功能及该界面按钮功能 ...

  3. 约跑APP测试

    项目名:约跑APP 用户需求规格说明书URL:http://www.cnblogs.com/liquan/p/6071804.html 组长博客URL:http://www.cnblogs.com/l ...

  4. VNC Server (CentOS 7 GNOME)

    1. 安装VNC服务 sudo yum install tigervnc-server -y 2. 启动VNC服务,设置密码,然后停止 vncserver :1 vncserver -kill :1 ...

  5. 检测web服务器指定位置大文件是否存在

    在bugscan群里看到有人问有一个大文件,想探测其是否存在.如果使用curl的话,会将整个文件下载到节点,对于扫描没有任何用处,反而浪费了扫描时间. 于是我想到的解决办法是不使用curl,直接用底层 ...

  6. SourceTree轻松Git项目

    这篇文档的目的是:让使用Git更轻松. 看完这篇文档你能做到的是: 1.简单的用Git管理项目. 2.怎样既要开发又要处理发布出去的版本bug情况. SourceTree是一个免费的Git图形化管理工 ...

  7. hdu1542 Atlantis (线段树+矩阵面积并+离散化)

    There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...

  8. oracle 月份中日的值必须介于 1 和当月最后一日之间

    解决方法: 1.用时间字段去关联字符串字段导致此错误.. 如果1.解决不了就看 2.把date'2017-01-01'  换成 to_date('2017-01-01','yyyy-mm-dd')

  9. MT【163】运动是相对的

    如图,在平面直角坐标系中,$P(6,8)$,四边形$ABCD$为矩形,$AB=16$,$AD=9$,点$A,B$分别在射线$OP$和$Ox$上,求$OD$的最大值_______            ...

  10. 【spring学习笔记二】Bean

    ### bean的三种实例化方式: 1.构造 2.静态工厂 3.实例工厂 其中,工厂就是工厂的概念,工厂函数factor-method会返回她生产出来的产品类. 而构造初始化也可以选择初始化方式和销毁 ...