obtain a tuple of all the subgroups matched : Group : Regular Expressions PYTHON TUTORIALS


PYTHON TUTORIALS » Regular Expressions » Group »

 

obtain a tuple of all the subgroups matched


import re

m = re.match('(www)-(ddd)', '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



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo Regular Expressions
» Group