公式ドキュメントから:
Regular Expression HOWTO - Grouping
Groups are numbered starting with 0. Group 0 is always present; it’s the whole RE, so match object methods all have group 0 as their default argument.
Subgroups are numbered from left to right, from 1 upward. Groups can be nested; to determine the number, just count the opening parenthesis characters, going from left to right.
import re
pattern = r"b(\d)|a(\d)"
sentence = "a4"
result = re.match(pattern, sentence)
if result:
print(result.group(0))
print(result.group(1, 2))
#
a4
(None, '4')
蛇足ながら、今回の場合は以下の様にしてもよいかと思います。
import re
pattern = r"[ab](\d)"
sentence = "a4"
result = re.match(pattern, sentence)
if result:
print(result.group())
print(result.group(1))
ちなみに regex · PyPI では同名の named group で capture が可能です。
import regex as re
pattern = r"b(?P<num>(\d))|a(?P<num>(\d))"
sentence = "a4"
result = re.match(pattern, sentence)
if result:
print(result.group())
print(result.group('num'))
#
a4
4