← index #8273Issue #1083
Off-topic · high · value 1.240
QUERY · ISSUE

re module defines re.match both as a function and class

openby Josverlopened 2022-02-07updated 2025-10-03
docs

There appears to be a conflict in both the documentation and the implementation of the re/ure module.

The documentation for the re module specifies that re.match is a function

.. function:: match(regex_str, string)
   Compile *regex_str* and match against *string*. Match always happens
   from starting position in a string.

However it also appears to document that re.match is a class, as there are methods documented.

I would expect that the class would be named Match rather then match

Match objects
-------------
Match objects as returned by `match()` and `search()` methods, and passed
to the replacement function in `sub()`.
.. method:: match.group(index)
   Return matching (sub)string. *index* is 0 for entire match,
   1 and above for each capturing group. Only numeric groups are supported.
.. method:: match.groups()

When checking the implementation ( v1.18) with the below code it appears that there is indeed a class named match returned from the function match.

import re
Substring ='.*Python'
String1 = "MicroPython"
m =re.match(Substring, String1)
print(type(m))
# MicroPython: <class 'match'>
# CPython: <class 're.Match'>

The reason that I noticed this is that I am creating and validating .pyi stubs that are autogenerated from the documentation, and in as part of test and validation noticed this conflict.

Its not to difficult to create another PR to update the documentation, however that would not match the current implementation.
However my main question is: should the implementation be updated to name the class 'Match' ?

CANDIDATE · ISSUE

unix-ffi re module throws when looking at match containing optional groups

openby dhouckopened 2026-02-18updated 2026-02-18

Problem description

When a regex contains optional capture groups, for example (a)?b, the PCREMatch.group() method throws an overflow error for that group. I believe this is because PCRE is representing a nonexistent group as SIZE_MAX and re isnʼt checking for that.

Additional fallout

The unix-ffi json library requires the unix-ffi re library, and currently cannot parse numbers unless they have an integer part, a fractional part, and and exponential part; instead, it throws this same error.

To reproduce

>>> import re
>>> r = re.compile(r'(a)?b')
>>> m = r.match('b')
>>> (m.group(0), m.group(1))

Expected (cpython, and micropython built-in re library):

('b', None)

Actual (micropython re library)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/micropython/re.py", line 65, in group
OverflowError: overflow converting long int to machine word

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied