← index #3440Issue #193
Related · high · value 1.286
QUERY · ISSUE

Exposing number of matches in a match object?

openby larsksopened 2017-11-15updated 2022-06-23
extmod

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?

CANDIDATE · ISSUE

re match object group() should be equivalent to group(0) according to Python3 documentation

closedby cefnopened 2017-07-05updated 2017-07-05

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.

1 comment
pfalcon · 2017-07-05

Thanks for the detailed report, fixed.

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