《python核心编程》读书笔记--第15章 正则表达式

15.1引言与动机

处理文本和数据是一件大事。正则表达式(RE)为高级文本匹配模式,为搜索-替换等功能提供了基础。RE是由一些字符和特殊符号组成的字符串,它们描述了这些字符和字符串的某种重复方式,因此能按某种模式匹配一个有相似特征的字符串的集合,也就是说,一个只能匹配一个字符串的RE是无聊的。

Python通过标准库的re模块支持正则表达式。

15.2正则表达式使用的特殊符号和字符

正则表达式中常见的字符列表。包括|、.、等,这个方面已经有很多讲解。比如这篇:http://blog.csdn.net/pleasecallmewhy/article/details/8929576(引用感谢)。

15.3正则表达式和Python语言

这一节将python中的re模块。主要讲match、search、compile等函数。

#-*- coding:utf-8 -*-
import re

#下面看一下match和group的基本用法
m = re.match('foo','foo')
if m is not None:
    print m.group()

m = re.match('foo','car')
if m is not None:
    print m.group()

m = re.match('foo','foo on the table')
print m.group()
print '-'*50
#下面是search和match的区别,search会从任意的地方开始搜索匹配模式
m = re.match('foo','seafood')
if m is not None:print m.group()  #匹配失败
m = re.search('foo','seafood')
if m is not None:print m.group()  #匹配成功
print '-'*50
bt = 'bat|bet|bit'
m = re.match(bt,'bat')
if m is not None:print m.group()
m = re.match(bt,'He bit me')
if m is not None:print m.group()  #匹配不成功
m = re.search(bt,'He bit me')
if m is not None:print m.group()  #匹配成功
print '-'*50
#下面将要说明句点不能匹配换行符
anyend = '.end'
m = re.match(anyend,'bend')
if m is not None:print m.group() #匹配成功
m = re.match(anyend,'\nend')
if m is not None:print m.group() #匹配不成功
m = re.search(anyend,'The end')
if m is not None:print m.group()
print '-'*50
#用转义字符表示句点
p1 = '3.14'
p2 = '3\.14'
print re.match(p1,'3.14').group() #成功,注意这里的.也属于‘任意字符’
print re.match(p1,'3014').group() #成功
print re.match(p2,'3.14').group() #成功
print re.match(p2,'3014').group() #出现错误

>>>
foo
foo
--------------------------------------------------
foo
--------------------------------------------------
bat
bit
--------------------------------------------------
Traceback (most recent call last):bend
 end
--------------------------------------------------
3.14
3014
3.14

  File "E:\NUT\PY\pycore\chapter15.py", line 45, in <module>
    print re.match(p2,'3014').group()
AttributeError: 'NoneType' object has no attribute 'group'
[Finished in 0.1s with exit code 1]
#-*- coding:utf-8 -*-
import re

#下面是创建字符集合 []和|的区别
m = re.match('[cr][23][dp][o2]','c3po')
print m.group() #成功匹配
print re.match('[cr][23][dp][o2]','c2do').group() #成功匹配
m = re.match('r2d2|c3po','c2do') #并不成功
if m is not None:print m.group()
print '-'*50
#重复、特殊字符和子组
#正则表达式最常见的情况包括特殊字符的使用,正则表达式模式的重复出现,以及使用圆括号对匹配模式进行分组和提取操作
patt = '\w+@(\w+\.)?\w+\.com'
print re.match(patt,'ored@xxx.com').group()
print re.match(patt,'ored@www.xxx.com').group()
print '-'*50
#下面看一下子组
m = re.match('(\w\w\w)-(\d\d\d)','abc-123')
print m.group()  #group返回所有匹配的内容
print m.group(1)
print m.group(2)
print m.groups() #groups返回所有子组组成的元组
#下面用几个例子展示group和groups的不同
print '-'*50
m = re.match('ab','ab') #没有子组
print m.group() #返回完全匹配
print m.groups() #返回空
m = re.match('(ab)','ab') #这样的形式是有一个子组
print m.group()
print m.groups()
print '-'*50
m = re.match('(a)(b)','ab')
print m.group()  #注意这里的机制是整体匹配
print m.groups()
m = re.match('(a(b))','ab')
print m.groups() #首先匹配外层,再匹配内层
>>>
c3po
c2do
--------------------------------------------------
ored@xxx.com
ored@www.xxx.com
--------------------------------------------------
abc-123
abc
123
('abc', '123')
--------------------------------------------------
ab
()
ab
('ab',)
--------------------------------------------------
ab
('a', 'b')
('ab', 'b')
[Finished in 0.1s]
posted @ 2016-02-13 22:28  司空格子Ored  阅读(684)  评论(0编辑  收藏  举报