regexpython-3.xundocumented-behavior

alternative to `regs` attribute of `match` object in `re` in python3


Is there an alternative to Match.regs? It is undocumented, and I see a proposal to remove it at http://bugs.python.org/issue18043, but it is useful to me. Here's it's usage:

> m = re.match('(a) (b)', 'a b ')
> m
<_sre.SRE_Match object; span=(0, 3), match='a b'>
> m.regs
((0, 3), (0, 1), (2, 3))

Is there an alternative way to obtain the spans of those groups for non-trivial regex?


Solution

  • Alternatives:

    >>> m.groups()
    ('a', 'b')
    >>> m.span()
    (0, 3)
    >>> m.span(1)
    (0, 1)
    >>> m.span(2)
    (2, 3)
    

    I wouldn't worry too much about it, though, as at this point it still exists in 3.5.