携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第7天,点击查看活动详情
前言
在本节中,我们将学习更多关于如何处理正则表达式的知识。在学习了基础知识之后,我们将更深入地了解模式元素,介绍另一种用于检索和解析字符串的方法 group()
方法,介绍如何搜索同一字符串的重复出现次数,以及处理更长的文本。
正则表达式实例
导入 re
模块,将电话模式匹配为 group
的一部分,并将其置于括号中,用于获取电话号码,符号 \d
用于匹配任意数字 (0
到 9
):
>>> match = re.search(r'My phone number is ([\d-]+)','My phone number is 100-0000-0000')
>>> match.group()
'My phone number is 100-0000-0000'
>>> match.group(0)
'My phone number is 100-0000-0000'
>>> match.group(1)
'100-0000-0000'
要定义组 group
,将定义的组放在括号中,以便后续可以单独检索组。使用 group()
或 group(0)
将获取整个匹配项,而括号中 group
的匹配项按它们出现的顺序排列,需要使用 group(1)
、group(2)
等。
我们也可以通过编译 (compile
) 模式进行匹配,并使用 re.IGNORECASE
选项捕获不区分大小写的模式,使用符号 \w
匹配任意字母(包括数字,但不包括句点等字符符号):
>>> pattern = re.compile(r'The judgement of this answer (\w+) is (wrong|correct)', re.IGNORECASE)
>>> match = pattern.search('I think The judgement of this answer five is wrong')
>>> match.group()
'The judgement of this answer five is wrong'
>>> match.group(1)
'five'
>>> match.group(2)
'wrong'
>>> match.groups()
('five', 'wrong')
>>> match = pattern.search('I think The judgement of this answer five$ is wrong')
>>> match.groups()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'groups'
使用编译模式的优势在于,如果需要反复多次匹配模式,可以节省时间,首先使用 compile
编译模式,然后使用该对象执行 research
方法。在编译模式时可以添加一些额外的标志选项,用于修改模式的匹配方式,例如使用 re.IGNORECASE
使模式不区分大小写。
匹配文本中出现的所有省和城市名,省名和城市名之间使用单个字符串分隔,且省、市名均以大写字母开头,使用符号 \s
匹配任意空白字符,包括制表符和其他空白特殊字符,而使用符号 .
则可以标记任意字符:
>>> pattern = re.compile(r'([A-Z][\w\s]+?).([A-Z][\w\s]+?)')
>>> text = 'Shandong,Jinan has a reputation as a famous "spring city", Guangdong.Guangzhou has a reputation as a famous "flower city", Jiangsu Suzhou has a reputation as a famous "water town".'
>>> list(pattern.finditer(text))
[<re.Match object; span=(0, 11), match='Shandong,Ji'>, <re.Match object; span=(59, 71), match='Guangdong.Gu'>, <re.Match object; span=(123, 133), match='Jiangsu Su'>]