0
点赞
收藏
分享

微信扫一扫

【Class 32】《Python编程快速上手》 查缺补漏四 第七章 模式匹配与正则表达式


1. 创建正则表达式对象 re.compile

Python 中所有正则表达式的函数都在 re 模块中。 import re

向 re.compile()传入一个字符串值,表示正则表达式,它将返回一个 Regex 模式对象(或者就简称为 Regex 对象)。

举例:匹配电话号码

import re
PhoneNumRegx = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')

str_ = PhoneNumRegx.search('My Number Is 400-111-1234,Thank You!')
print( str_.group() ) ### 400-111-1234

2. 利用括号分组

向 group()匹配对象方法传入整数 1 或 2,就可以取得匹配文本的不同部分。

向 group()方法传入 0 或不传入参数,将返回整个匹配的文本。

import re

PhoneNumRegx = re.compile(r'(\d\d\d)-(\d\d\d)-(\d\d\d\d)')
str_ = PhoneNumRegx.search('My Number Is 400-111-1234,Thank You!')

print( str_.group()) # 400-111-1234
print( str_.group(0)) # 400-111-1234
print( str_.group(1)) # 400
print( str_.group(2)) # 111
print( str_.group(3)) # 1234

3. 用管道匹配多个分组

字符|称为“管道”。希望匹配许多表达式中的一个时,就可以使用它。

如果查找的字符在被查找的字符串中同时出现,则第一次出现的匹配文本将作为Match 对象返回。

>>> heroRegex = re.compile (r'Batman|Tina Fey')
>>> mo1 = heroRegex.search('Batman and Tina Fey.')
>>> mo1.group()
'Batman'
>>> mo2 = heroRegex.search('Tina Fey and Batman.')
>>> mo2.group()
'Tina Fey'

如果同时出现了多个字符串,而希望找到所有匹配的字符串,则应该使用 re.findall()

4. 管道匹配指定前缀:

mo.group()返回了完全匹配的文本’Batmobile’,

而 mo.group(1)只是返回第一个括号分组内匹配的文本’mobile’

>>> batRegex = re.compile(r'Bat(man|mobile|copter|bat)')
>>> mo = batRegex.search('Batmobile lost a wheel')
>>> mo.group()
'Batmobile'
>>> mo.group(1)
'mobile'

5. 用问号实现可选匹配

不论这段文本在不在,正则表达式都会认为匹配

>>> batRegex = re.compile(r'Bat(wo)?man') 
>>> mo1 = batRegex.search('The Adventures of Batman')
>>> mo1.group()
'Batman'
>>> mo2 = batRegex.search('The Adventures of Batwoman')
>>> mo2.group()
'Batwoman'

6. 用星号匹配零次或多次

>>> batRegex = re.compile(r'Bat(wo)*man')
>>> mo1 = batRegex.search('The Adventures of Batman')
>>> mo1.group()
'Batman'
>>> mo2 = batRegex.search('The Adventures of Batwoman')
>>> mo2.group()
'Batwoman'
>>> mo3 = batRegex.search('The Adventures of Batwowowowoman')
>>> mo3.group()
'Batwowowowoman'

7. 用花括号匹配特定次数

(Ha){3,}将匹配 3 次或更多次实例

(Ha){,5}将匹配 0 到 5 次实例

>>> haRegex = re.compile(r'(Ha){3}')
>>> mo1 = haRegex.search('HaHaHa')
>>> mo1.group()
'HaHaHa'
>>> mo2 = haRegex.search('Ha')
>>> mo2 == None
True

8. 贪心和非贪心匹配

Python 的正则表达式默认是“贪心”的,这表示在有二义的情况下,它们会尽可能匹配最长的字符串。

花括号的“非贪心”版本匹配尽可能最短的字符串,即在结束的花括号后跟着一个问号。

贪心匹配: (Ha){,5} 尽可能打印5个Ha 的匹配

非贪心匹配: (Ha){,5}? 尽可能打印最短的Ha 的匹配,即便字符串为:“iHaHaHaHaHa”, 也只会打印第一个Ha

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

9. re.findall()方法

除了search方法外,Regex对象也有一个findall()方法。

search() 将返回一个Match对象,包含被查找字符串中的“第一次”匹配的文本,

findall() 方法将返回一组字符串,包含被查找字符串中的所有匹配。

>>> phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
>>> mo = phoneNumRegex.search('Cell: 415-555-9999 Work: 212-555-0000')
>>> mo.group()
'415-555-9999'

>>> phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d') # has no groups
>>> phoneNumRegex.findall('Cell: 415-555-9999 Work: 212-555-0000')
['415-555-9999', '212-555-0000']

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

每个元组表示一个找到的匹配,其中的项就是正则表达式中每个分组的匹配字符串。

>>> phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d)-(\d\d\d\d)') # has groups
>>> phoneNumRegex.findall('Cell: 415-555-9999 Work: 212-555-0000')
[('415', '555', '1122'), ('212', '555', '0000')]

10. 正则表达式的元字符

可以参考百度 ​​​​https://docs.python.org/2.7/library/re.html​​​​

或者参考我的博文:​​【Class 13】python 正则表达式 re.findall​​

11. r’^\d+$’ 匹配从开始到结束都是数字的字符串

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

12. 通配字符 .

(句点)字符称为“通配符”。它匹配除了换行之外的所有字符。

**13. 点星 (.*) : 匹配所有字符 **

点-星将匹配除换行外的所有字符。

通过传入 re.DOTALL 作为 re.compile()的第二个参数,可以让句点字符匹配所有字符,包括换行字符。

>>> noNewlineRegex = re.compile('.*')
>>> noNewlineRegex.search('Serve the public trust.\nProtect the innocent. \nUphold the law.').group()
'Serve the public trust.'
>>> newlineRegex = re.compile('.*', re.DOTALL)
>>> newlineRegex.search('Serve the public trust.\nProtect the innocent.\nUphold the law.').group()
'Serve the public trust.\nProtect the innocent.\nUphold the law.'

14. 不区分大小写的匹配

要让正则表达式不区分大小写,可以向 re.compile()传入 re.IGNORECASE 或 re.I,作为第二个参数。

>>> robocop = re.compile(r'robocop', re.I)
>>> robocop.search('RoboCop is part man, part machine, all cop.').group()
'RoboCop'
>>> robocop.search('ROBOCOP protects the innocent.').group()
'ROBOCOP'
>>> robocop.search('Al, why does your programming book talk about robocop so much?').group()
'robocop'

15. 用 sub()方法替换字符串

正则表达式不仅能找到文本模式,而且能够用新的文本替换掉这些模式。

Regex对象的 sub()方法需要传入两个参数。

第一个参数是一个字符串,用于取代发现的匹配。

第二个参数是一个字符串,即正则表达式。sub()方法返回替换完成后的字符串。

>>> namesRegex = re.compile(r'Agent \w+')
>>> namesRegex.sub('CENSORED', 'Agent Alice gave the secret documents to Agent Bob.')
'CENSORED gave the secret documents to CENSORED.'

16. 管理复杂的正则表达式

匹配复杂的文本模式,可能需要长的、费解的正则表达式。

你可以告诉 re.compile(),忽略正则表达式字符串中的空白符和注释,从而缓解这一点。

要实现这种详细模式,可以向 re.compile()传入变量 re.VERBOSE,作为第二个参数

举例:

## 这是一段很负责的正则表达式,很难看
phoneRegex = re.compile(r'((\d{3}|\(\d{3}\))?(\s|-|\.)?\d{3}(\s|-|\.)\d{4}(\s*(ext|x|ext.)\s*\d{2,5})?)')

## 可以将正则表达式放在多行中,并加上注释
phoneRegex = re.compile(r'''(
(\d{3}|\(\d{3}\))? # area code
(\s|-|\.)? # separator
\d{3} # first 3 digits
(\s|-|\.) # separator
\d{4} # last 4 digits
(\s*(ext|x|ext.)\s*\d{2,5})? # extension
)''', re.VERBOSE)
## 请注意,前面的例子使用了三重引号('"),创建了一个多行字符串。

17. 组合使用 re.IGNOREC ASE、re.DOTALL 和 re.VERBOSE


someRegexValue = re.compile(‘foo’, re.IGNORECASE | re.DOTALL | re.VERBOSE)




举报

相关推荐

第七章 正则表达式

0 条评论