Exposing number of matches in a match object?
The Match objects returned by re.match and re.search internally stores the number of match groups:
>>> r = re.compile('hello my ([^ ]+) is ([^ .]+).')
>>> m = r.search('hello my name is lars')
>>> m
<match num=3>
This value isn't currently exposed in Python. The workaround for iterating through available matches, which we see in some of the tests, is sort of clunky:
def print_groups(match):
print('----')
try:
i = 0
while True:
print(match.group(i))
i += 1
except IndexError:
pass
I've read through the contributor guidelines, and since there exists a workaround I understand that simply exposing the match count may not be a priority. Would you accept a documentation pr that includes some variant of the above example in the ure module documentation?
re module defines re.match both as a function and class
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' ?