Python 编程快速上手 第 7章 模式匹配与正则表达式

1 >>> consonantRegex = re.compile(r'[^aeiouAEIOU]')
2 >>> consonantRegex.findall('RoboCop eats baby food. BABY FOOD.')
3 ['R', 'b', 'c', 'p', ' ', 't', 's', ' ', 'b', 'b', 'y', ' ', 'f', 'd', '.', '
4 ', 'B', 'B', 'Y', ' ', 'F', 'D', '.'] 

7.1 不用正则表达式来查找文本模式

7.2 用正则表达式查找文本模式

正则表达式,简称为 regex,是文本模式的描述方法

7.2.1 创建正则表达式对象

7.2.2 匹配 Regex 对象

1 >>> phoneNumRegex = re.compile(r'ddd-ddd-dddd')
2 >>> mo = phoneNumRegex.search('My number is 415-555-4242.')
3 >>> print('Phone number found: ' + mo.group())
4 
5 
6 Phone number found: 415-555-4242 

7.2.3 正则表达式匹配复习

1.用 import re 导入正则表达式模块。

2.用 re.compile()函数创建一个 Regex 对象(记得使用原始字符串)。

3.向 Regex 对象的 search()方法传入想查找的字符串。它返回一个 Match 对象。

4.调用 Match 对象的 group()方法,返回实际匹配文本的字符串。

7.3 用正则表达式匹配更多模式

7.3.1 利用括号分组

>>> phoneNumRegex = re.compile(r'(ddd)-(ddd-dddd)')
>>> mo = phoneNumRegex.search('My number is 415-555-4242.')
>>> mo.group(1)
'415'
>>> mo.group(2)
'555-4242'
>>> mo.group(0)
'415-555-4242'
>>> mo.group()
'415-555-4242' 
#如果想要一次就获取所有的分组,请使用 groups()方法,注意函数名的复数形式
>>> mo.groups()
('415', '555-4242')
>>> areaCode, mainNumber = mo.groups()
>>> print(areaCode)
415
>>> print(mainNumber)
555-4242

7.3.2 用管道匹配多个分组

1 >>> heroRegex = re.compile (r'Batman|Tina Fey')
2 >>> mo1 = heroRegex.search('Batman and Tina Fey.')
3 >>> mo1.group()
4 'Batman'
5 >>> mo2 = heroRegex.search('Tina Fey and Batman.')
6 >>> mo2.group()
7 'Tina Fey'
8 #Batman 和 Tina Fey 都出现在被查找的字符串中,第一次出现的匹配文本,
9 将作为 Match 对象返回。
1 >>> batRegex = re.compile(r'Bat(man|mobile|copter|bat)')
2 >>> mo = batRegex.search('Batmobile lost a wheel')
3 >>> mo.group()
4 'Batmobile'
5 >>> mo.group(1)
6 'mobile'

7.3.3 用问号实现可选匹配

1 >>> phoneRegex = re.compile(r'(ddd-)?ddd-dddd')
2 >>> mo1 = phoneRegex.search('My number is 415-555-4242')
3 >>> mo1.group()
4 '415-555-4242'
5 >>> mo2 = phoneRegex.search('My number is 555-4242')
6 >>> mo2.group()
7 '555-4242'

7.3.4 用星号匹配零次或多次

 1 >>> batRegex = re.compile(r'Bat(wo)*man')
 2 >>> mo1 = batRegex.search('The Adventures of Batman')
 3 >>> mo1.group()
 4 'Batman'
 5 >>> mo2 = batRegex.search('The Adventures of Batwoman')
 6 >>> mo2.group()
 7 'Batwoman'
 8 >>> mo3 = batRegex.search('The Adventures of Batwowowowoman')
 9 >>> mo3.group()
10 'Batwowowowoman'

7.3.5 用加号匹配一次或多次

7.3.6 用花括号匹配特定次数

1 >>> haRegex = re.compile(r'(Ha){3}')
2 >>> mo1 = haRegex.search('HaHaHa')
3 >>> mo1.group()
4 'HaHaHa'
5 >>> mo2 = haRegex.search('Ha')
6 >>> mo2 == None
7 True

7.4 贪心和非贪心匹配

1 >>> greedyHaRegex = re.compile(r'(Ha){3,5}')
2 >>> mo1 = greedyHaRegex.search('HaHaHaHaHa')
3 >>> mo1.group()
4 'HaHaHaHaHa'
5 >>> nongreedyHaRegex = re.compile(r'(Ha){3,5}?')
6 >>> mo2 = nongreedyHaRegex.search('HaHaHaHaHa')
7 >>> mo2.group()
8 'HaHaHa'

7.5 findall()方法

1 >>> phoneNumRegex = re.compile(r'ddd-ddd-dddd') # has no groups
2 >>> phoneNumRegex.findall('Cell: 415-555-9999 Work: 212-555-0000')
3 ['415-555-9999', '212-555-0000'] 

如果在正则表达式中有分组,那么 findall 将返回元组的列表

1 >>> phoneNumRegex = re.compile(r'(ddd)-(ddd)-(dddd)') # has groups
2 >>> phoneNumRegex.findall('Cell: 415-555-9999 Work: 212-555-0000')
3 [('415', '555', '1122'), ('212', '555', '0000')]

 Python 编程快速上手 第 7章 模式匹配与正则表达式

7.7 建立自己的字符分类

字符分类[aeiouAEIOU]将匹配所有元音字 符,不论大小写

1 >>> vowelRegex = re.compile(r'[aeiouAEIOU]')
2 >>> vowelRegex.findall('RoboCop eats baby food. BABY FOOD.')
3 ['o', 'o', 'o', 'e', 'a', 'a', 'o', 'o', 'A', 'O', 'O']

通过在字符分类的左方括号后加上一个插入字符(^),就可以得到“非字符类”。 非字符类将匹配不在这个字符类中的所有字符

1 >>> consonantRegex = re.compile(r'[^aeiouAEIOU]')
2 >>> consonantRegex.findall('RoboCop eats baby food. BABY FOOD.')
3 ['R', 'b', 'c', 'p', ' ', 't', 's', ' ', 'b', 'b', 'y', ' ', 'f', 'd', '.', '
4 ', 'B', 'B', 'Y', ' ', 'F', 'D', '.'] 

7.8 插入字符和美元字符

正则表达式 r'^Hello'匹配以'Hello'开始的字符串。

1 >>> beginsWithHello = re.compile(r'^Hello')
2 >>> beginsWithHello.search('Hello world!')
3 <_sre.SRE_Match object; span=(0, 5), match='Hello'>
4 >>> beginsWithHello.search('He said hello.') == None
5 True

正则表达式 r'd$'匹配以数字 0 到 9 结束的字符串

1 >>> endsWithNumber = re.compile(r'd$')
2 >>> endsWithNumber.search('Your number is 42')
3 <_sre.SRE_Match object; span=(16, 17), match='2'>
4 >>> endsWithNumber.search('Your number is forty two.') == None
5 True

正则表达式 r'^d+$'匹配从开始到结束都是数字的字符串

1 >>> wholeStringIsNum = re.compile(r'^d+$')
2 >>> wholeStringIsNum.search('1234567890')
3 <_sre.SRE_Match object; span=(0, 10), match='1234567890'>
4 >>> wholeStringIsNum.search('12345xyz67890') == None
5 True
6 >>> wholeStringIsNum.search('12 34567890') == None
7 True