import re
some_regex = re.compile("^foo(?P<idx>[0-9]*)?$")

match = some_regex.match('foo11')
print(match and 'idx' in match.groupdict())

match = some_regex.match('bar11')
print(match and 'idx' in match.groupdict())