In python regular expressions(regex) groups() method is used to fetch the captured groups from a match object. The groups() method returns all the captured groups as tuples.
Sample Code:
import re
text = “Hello Everyone, This is Ajay and i am 30 year old”
pattern= r”Hello Everyone, This is (\w+) and i am (\d+) year old”
match=re.search(pattern,text)
if match:
groups=match.groups()
print(groups)
Output -> (‘Ajay’, ‘30’)
The Groups used in the pattern are:
(\w+) : captures one or more word characters
(\d+): captures one or more digits