obtain a tuple of all the subgroups matched: : group « Regular Expressions « Python Tutorial






import re

m = re.match('(\w\w\w)-(\d\d\d)', 'abc-123')
m.group()                      # entire match

m.group(1)                     # subgroup 1

m.group(2)                     # subgroup 2

m.groups()                     # all subgroups

m = re.match('ab', 'ab')       # no subgroups
m.group()                      # entire match

m.groups()                     # all subgroups


m = re.match('(ab)', 'ab')     # one subgroup
m.group()                      # entire match

m.group(1)                     # subgroup 1

m.groups()                     # all subgroups

m = re.match('(a)(b)', 'ab')   # two subgroups
m.group()                      # entire match
m.group(1)                     # subgroup 1
m.group(2)                     # subgroup 2
m.groups()                     # all subgroups

m = re.match('(a(b))', 'ab')         # two subgroups
m.group()                      # entire match

m.group(1)                     # subgroup 1
m.group(2)                     # subgroup 2
m.groups()                     # all subgroups








16.3.group
16.3.1.obtain a tuple of all the subgroups matched:
16.3.2.grouping and greedy operations.