Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Doc/library/re.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1407,10 +1407,10 @@ when there is no match, you can test whether there was a match with a simple
result is a single string; if there are multiple arguments, the result is a
tuple with one item per argument. Without arguments, *group1* defaults to zero
(the whole match is returned). If a *groupN* argument is zero, the corresponding
return value is the entire matching string; if it is in the inclusive range
[1..99], it is the string matching the corresponding parenthesized group. If a
group number is negative or larger than the number of groups defined in the
pattern, an :exc:`IndexError` exception is raised. If a group is contained in a
return value is the entire matching string; if it is a positive integer, it is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually there seems to be a limit and it can be quantized on one's machine:

>>> import _sre
>>> _sre.MAXGROUPS
1073741823

I see multiple guards in Lib/sre_parse.py that use the MAXGROUPS constant for the upper bound.

Maybe the docs should say in the inclusive range [1.. _sre.MAXGROUPS] or similar?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_sre.MAXGROUPS limits the number of groups allowed in a pattern. This is enforced at compile time in _parser.py.

The .group() method itself does not check against _sre.MAXGROUPS. It only checks whether the requested group index is within the number of groups defined in that specific pattern (see match_getindex, where it checks i >= self->groups).

The next sentence in the documentation already explains this clearly:
"If a group number is negative or larger than the number of groups defined in the pattern, an IndexError exception is raised."

Mentioning _sre.MAXGROUPS here would reference a private module and mix two different things:

  • the compile-time limit on total groups in a pattern
  • the runtime behavior of .group().

So I think it is better not to mention _sre.MAXGROUPS in this context.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct in making that distinction.

LGTM! 👍

the string matching the corresponding parenthesized group. If a group number is
negative or larger than the number of groups defined in the pattern, an
:exc:`IndexError` exception is raised. If a group is contained in a
part of the pattern that did not match, the corresponding result is ``None``.
If a group is contained in a part of the pattern that matched multiple times,
the last match is returned. ::
Expand Down
Loading