re.MatchObject.group() function in Python Regex
re.MatchObject.group() method returns the complete matched subgroup by default or a tuple of matched subgroups depending on the number of arguments
Syntax: re.MatchObject.group([group])
Parameter:
- group: (optional) group defaults to zero (meaning that it it will return the complete matched string). Return -1 if group exists but did not contribute to the match.
Return: Complete match by default else one or more matched subgroups depending on the arguments.
IndexError: If the group number passed as argument is negative or greater than the number of groups defined in the match pattern then an IndexError exception will be raised
AttributeError: If a matching pattern is not found then it raise AttributeError.
Consider the below example:
Example 1:
A program to print the username, company_name and domain from a emailID
Python3
import re
match_object = re.match(r '(\w+)@(\w+)\.(\w+)' , 'username@geekforgeeks.org' )
print (match_object.group())
print (match_object.group( 1 ))
print (match_object.group( 2 ))
print (match_object.group( 3 ))
print (match_object.group( 1 , 2 , 3 ))
|
Output:
username@geekforgeeks.org
username
geekforgeeks
org
('username', 'geekforgeeks', 'org')
It’s time to understand the above program. We use a re.match() method to find a match in the given string(‘username@geekforgeeks.org‘) the ‘w‘ indicates that we are searching for an alphabetical character and the ‘+‘ indicates that we are searching for continuous alphabetical characters in the given string. Note the use of ‘()‘ the parenthesis is used to define different subgroups, in the above example, we have three subgroups in the match pattern. The result we get is a re.MatchObject which is stored in match_object.
Note: To know more about regex patterns refer Python regex
Depending on the arguments passed the group method returns us different strings and also it returns a tuple of matched strings.
Example 2:
If we pass an invalid group number in the method argument then we will get an IndexError exception.
Python3
import re
match_object = re.match(r '(\w+)@(\w+)\.(\w+)' , 'username@geekforgeeks.org' )
print (match_object.group( 7 ))
|
Output:
Traceback (most recent call last):
File "/home/8da42759204c98da7baa88422a4a74e0.py", line 17, in
print(match_object.group(7))
IndexError: no such group
Last Updated :
27 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...