all Python 3refunction
re.Match.groups()
groups
Return a tuple of all captured groups of a Match object (group 0 excluded); groups that did not participate get default (None by default).
Syntax
m.groups(default=None)
Group 0 — the whole match — is deliberately left out; ask m.group(0) for it. A group that did not participate comes back as None, which blows up exactly where you concatenate strings or call int(), so either check it or pass groups('') to get empty strings instead. A pattern with no groups yields an empty tuple, not None.
Examples
import re
m = re.match(r'(\w+)@(\w+)', 'user@host')
m.groups() # → ('user', 'host')
m2 = re.match(r'(a)(b)?', 'a')
m2.groups() # → ('a', None)
m2.groups(default='-') # → ('a', '-')
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy