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 match object group() should be equivalent to group(0) according to Python3 documentation
Trying to port the ijson module to Micropython, I substituted the re module downloaded via upip for the standard re module referenced by ijson.
I found that ijson at line 34 of https://github.com/isagalaev/ijson/blob/master/ijson/backends/python.py relied on the following contract of the match object, by invoking group() with no arguments...
"Without arguments, group1 defaults to zero (the whole match is returned)." ( from https://docs.python.org/3/library/re.html )
Unfortunately the micropython re module doesn't conform. I was able to fix the library by invoking group(0) in the place of group() but ideally the micropython-lib re module would be compliant.
Thanks for the detailed report, fixed.