Skip to content

Conversation

@mohsinm-dev
Copy link
Contributor

@mohsinm-dev mohsinm-dev commented Feb 11, 2026

The documentation for re.Match.group() incorrectly states that numeric group arguments must be in the inclusive range [1..99]. This limit was removed in Python 3.5 (bpo-22437) when the number of capturing groups became dynamic.

Replace [1..99] with "a positive integer" since the next sentence already documents the IndexError for out-of-range values.


📚 Documentation preview 📚: https://cpython-previews--144696.org.readthedocs.build/

The documentation incorrectly stated that numeric group arguments
must be in the range [1..99]. This limit was removed in Python 3.5
(bpo-22437). Replace with "a positive integer" since the next
sentence already documents the IndexError for out-of-range values.
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM. 👍

@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes and removed awaiting merge labels Feb 11, 2026
[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! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

3 participants