ZetCode

Python Match.endpos 属性

最后修改于 2025 年 4 月 20 日

Match.endpos 简介

Match.endpos 属性是 Python 的 re 模块的一部分。它表示正则表达式引擎停止搜索的字符串中的结束位置。

此属性在创建匹配对象时设置,并反映搜索边界。它对于理解模式匹配的范围很有用。

默认情况下,该值等于字符串的长度,但可以使用搜索方法中的 posendpos 参数进行修改。

基本语法

Match.endpos 属性从匹配对象访问

match.endpos

它返回一个整数,表示搜索的结束索引。这是被考虑的最后一个字符之后的索引。

基本的 Match.endpos 示例

让我们从一个简单的示例开始,展示默认行为。

basic_endpos.py
#!/usr/bin/python

import re

text = "Python programming is fun"
pattern = re.compile(r'programming')
match = pattern.search(text)

print(f"Match found: {match.group()}")
print(f"End position: {match.endpos}")
print(f"String length: {len(text)}")

此示例表明,在未指定搜索边界时,endpos 与字符串长度匹配。该属性是只读的。

match = pattern.search(text)

搜索创建一个匹配对象。默认情况下,它搜索整个字符串。在这种情况下,endpos 将等于字符串的长度。

print(f"End position: {match.endpos}")

这将打印搜索的结束位置,与搜索整个字符串时的 len(text) 相同。

使用 endpos 和搜索边界

我们可以使用 endpos 参数限制搜索范围。

endpos_boundary.py
#!/usr/bin/python

import re

text = "Python programming is fun"
pattern = re.compile(r'is')
match = pattern.search(text, 0, 18)  # endpos=18

print(f"Match found: {match.group()}")
print(f"End position: {match.endpos}")
print(f"String length: {len(text)}")

在这里,我们将搜索限制在前 18 个字符。即使字符串更长,匹配对象的 endpos 也反映了此边界。

endpos 和多个匹配项

使用 finditer 时,每个匹配对象都具有相同的 endpos

multiple_matches.py
#!/usr/bin/python

import re

text = "apple banana apple orange apple"
pattern = re.compile(r'apple')
matches = pattern.finditer(text, 0, 20)  # endpos=20

for match in matches:
    print(f"Found '{match.group()}' at {match.start()}")
    print(f"Search end position: {match.endpos}")

这表明来自同一搜索的所有匹配对象共享相同的 endpos 值。由于边界,只找到两个匹配项。

endpos 和 Fullmatch

fullmatch 方法受 endpos 影响。

fullmatch_example.py
#!/usr/bin/python

import re

text = "Python3"
pattern = re.compile(r'Python\d')
match = pattern.fullmatch(text, 0, 6)  # endpos=6

print(f"Match: {match}")  # None, as 'Python' doesn't match fully
match = pattern.fullmatch(text, 0, 7)  # endpos=7
print(f"Match: {match.group()}")

fullmatch 要求整个字符串(直到 endpos)匹配。在这里,我们看到 endpos 如何影响结果。

endpos 和子字符串

endpos 也适用于字符串切片。

substring_example.py
#!/usr/bin/python

import re

text = "Python programming is fun"
substring = text[:18]  # First 18 characters
pattern = re.compile(r'is')
match = pattern.search(substring)

print(f"Match found: {match.group()}")
print(f"End position: {match.endpos}")
print(f"Substring length: {len(substring)}")

这表明在使用切片搜索时,endpos 反映了子字符串的长度。该行为与使用 endpos 参数匹配。

endpos 和多行字符串

在多行模式下,endpos 仍然标记绝对结束位置。

multiline_example.py
#!/usr/bin/python

import re

text = """First line
Second line
Third line"""
pattern = re.compile(r'line$', re.MULTILINE)
matches = pattern.finditer(text, 0, 25)  # endpos=25

for match in matches:
    print(f"Found '{match.group()}' at {match.start()}")
    print(f"Search end position: {match.endpos}")

即使在多行模式下,endpos 也限制了绝对字符位置。仅返回此位置之前的匹配项。

endpos 和重叠匹配

endpos 也会影响重叠匹配。

overlapping_example.py
#!/usr/bin/python

import re

text = "ababababab"
pattern = re.compile(r'(?=(aba))')
matches = pattern.finditer(text, 0, 8)  # endpos=8

for match in matches:
    print(f"Found '{match.group(1)}' at {match.start(1)}")
    print(f"Search end position: {match.endpos}")

这显示了 endpos 如何限制重叠匹配。先行断言会查找指定位置之前的匹配项。

最佳实践

使用 Match.endpos 时,请考虑以下最佳实践

性能注意事项

通过减少搜索空间,使用 endpos 可以提高性能。这对于大型字符串尤其有价值。

性能优势随着完整字符串和有限搜索范围之间的大小差异而增加。始终在优化时进行测量。

来源

Python Match.endpos 文档

本教程介绍了 Python 的 Match.endpos 属性的essential 方面。了解搜索边界有助于创建高效的正则表达式模式。

作者

我叫 Jan Bodnar,是一位充满热情的程序员,拥有丰富的编程经验。 我自 2007 年以来一直在撰写编程文章。 迄今为止,我已经撰写了 1,400 多篇文章和 8 本电子书。 我在编程教学方面拥有超过十年的经验。

列出所有 Python 教程