如何在 Python 的正则表达式中找到每个匹配的精确位置?


介绍

我们在 Python 中使用 re 模块来处理正则表达式。文本搜索和更复杂的文本操作都使用正则表达式。像 grep 和 sed 这样的工具,像 vi 和 emacs 这样的文本编辑器,以及像 Tcl、Perl 和 Python 这样的计算机语言都内置了正则表达式支持。

Python 中的 re 模块提供了用于匹配正则表达式的函数。

定义我们要查找或修改的文本的正则表达式称为模式。此字符串由文本文字和元字符组成。compile 函数用于创建模式。建议使用原始字符串,因为正则表达式经常包含特殊字符。(r 字符用于指示原始字符串。)在这些字符被组装成模式之前,不会以这种方式解释它们。

在模式组装后,可以使用其中一个函数将模式应用于文本字符串。可用的函数包括 Match、search、find 和 finditer。

使用语法

这里使用的正则表达式函数是:我们用正则表达式函数查找匹配项。

re.match(): Determines if the RE matches at the beginning of the string. If zero or more characters at the beginning of the string match the regular expression pattern, the match method returns a match object.

p.finditer(): Finds all substrings where the RE matches and returns them as an iterator. An iterator delivering match objects across all non-overlapping matches for the pattern in a string is the result of the finditer method.

re.compile(): Compile a regular expression pattern into a regular expression object, which can be used for matching using its match(), search(), and other methods described below. The expression’s behavior can be modified by specifying a flag's value. Values can be any of the following variables combined using bitwise OR (the | operator).

m.start(): m.start() returns the offset in the string at the match's start.

m.group(): You may use the multiple-assignment approach to assign each value to a different variable when mo.groups() returns a tuple of values, as in the areaCode, mainNumber = mo.groups() line below.

search: It is comparable to re.match() but does not require that we just look for matches at the beginning of the text. The search() function can locate a pattern in the string at any location, but it only returns the first instance of the pattern.

算法

  • 使用 import re 导入正则表达式模块。

  • 使用 re.compile() 函数创建一个正则表达式对象。(请记住使用原始字符串。)

  • 将要搜索的字符串传递到正则表达式对象的 finditer() 方法中。这将返回一个 Match 对象。

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

  • 我们还可以使用 span() 方法获得单个元组中的起始和结束索引。

示例

#importing re functions import re #compiling [A-Z0-9] and storing it in a variable p p = re.compile("[A-Z0-9]") #looping m times in p.finditer for m in p.finditer('A5B6C7D8'): #printing the m.start and m.group print m.start(), m.group()

输出

这将给出以下输出:

0 A
1 5
2 B
3 6
4 C
5 7
6 D
7 8

代码解释

使用 import re 导入正则表达式模块。使用 re.compile() 函数创建一个正则表达式对象(“[A-Z0-9]”),并将其赋值给变量 p。为 m 运行循环,并将要搜索的字符串传递到正则表达式对象的 finditer() 方法中。这将返回一个 Match 对象。调用 Match 对象的 m.group() 和 m.start() 方法以返回实际匹配文本的字符串。

示例

# Python program to illustrate # Matching regex objects # with groups import re phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)') mo = phoneNumRegex.search('My number is 415-555-4242.') print(mo.groups())

输出

这将给出以下输出:

('415', '555-4242')

代码解释

使用 import re 导入正则表达式模块。使用 re.compile() 函数创建一个正则表达式对象(r'(\d\d\d)-(\d\d\d-\d\d\d\d)'),并将其赋值给变量 phoneNumRegex。将要搜索的字符串传递到正则表达式对象的 search() 方法中,并将其存储在变量 mo 中。这将返回一个 Match 对象。调用 Match 对象的 mo.groups() 方法以返回实际匹配文本的字符串。

结论

Python re 模块提供的 search()、match() 和 finditer() 方法允许我们匹配正则表达式模式,如果匹配成功,它将提供 Match 对象实例。使用此 Match 对象利用 start()、end() 和 span() 方法检索有关匹配字符串的详细信息。

当存在多个匹配项时,如果您使用 findall() 加载所有匹配项,则存在超载 RAM 的风险。您可以获得所有潜在匹配项的形式的迭代器对象,而不是使用 finditer() 方法,这将提高效率。

这意味着 finditer() 提供了一个可调用的对象,当调用时,它将把结果加载到内存中。

更新于: 2022 年 9 月 20 日

2K+ 浏览量

启动您的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.