re.MatchObject.groups() function in Python - Regex Last Updated : 29 Aug, 2020 Comments Improve Suggest changes Like Article Like Report This method returns a tuple of all matched subgroups. Syntax: re.MatchObject.groups() Return: A tuple of all matched subgroups AttributeError: If a matching pattern is not found then it raise AttributeError. Consider the below example: Example 1: Python3 import re """We create a re.MatchObject and store it in match_object variable the '()' parenthesis are used to define a specific group""" match_object = re.match(r'(\d+)\-(\w+)\-(\w+)', '498-ImperialCollege-London') """ d in above pattern stands for numerical character w in above pattern stands for alphabetical character + is used to match a consecutive set of characters satisfying a given condition so w+ will match a consecutive set of alphabetical characters d+ will match a consecutive set of numerical characters """ # generating the tuple with all matched groups detail_tuple = match_object.groups() # printing the tuple print(detail_tuple) Output: ('498', 'ImperialCollege', 'London') It's time to understand the above program. We use a re.match() method to find a match in the given string('498-ImperialCollege-London') the 'w' indicates that we are searching for a alphabetical character and the '+' indicates that we are searching for continuous alphabetical characters in the given string. Similarly d+ will match a consecutive set of numerical characters.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 variable. Example 2: If a match object is not found then it raises AttributeError. Python3 import re """We create a re.MatchObject and store it in match_object variable the '()' parenthesis are used to define a specific group""" match_object = re.match(r'(\d+)\-(\w+)\-(\w+)', '1273984579846') """ w in above pattern stands for alphabetical character + is used to match a consecutive set of characters satisfying a given condition so 'w+' will match a consecutive set of alphabetical characters """ # Following line will raise AttributeError exception print(match_object.groups()) Output: Traceback (most recent call last): File "/home/6510bd3e713a7b9a5629b30325c8a821.py", line 18, in print(match_object.groups()) AttributeError: 'NoneType' object has no attribute 'groups' Comment More infoAdvertise with us Next Article re.MatchObject.groups() function in Python - Regex H haridarshanc Follow Improve Article Tags : Python python-regex Practice Tags : python Similar Reads 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). 3 min read re.MatchObject.groupdict() function in Python - Regex This method returns a dictionary with the groupname as keys and the matched string as the value for that key. Syntax: re.MatchObject.groupdict() Return: A dictionary with groupnames as the keys and matched string as the value for the key. AttributeError: If a matching pattern is not found then it ra 3 min read re.MatchObject.span() Method in Python - regex re.MatchObject.span() method returns a tuple containing starting and ending index of the matched string. If group did not contribute to the match it returns(-1,-1). Syntax: re.MatchObject.span() Parameters: group (optional) By default this is 0. Return: A tuple containing starting and ending index o 2 min read Python Regex - re.MatchObject.start() and re.MatchObject.end() functions In this article, we are going to see re.MatchObject.start() and re.MatchObject.end() regex methods. re.MatchObject.start() This method returns the first index of the substring matched by group. Syntax: re.MatchObject.start([group]) Parameter: group: (optional) group defaults to zero (meaning the who 2 min read Python Regex: Replace Captured Groups Regular Expressions, often abbreviated as Regex, are sequences of characters that form search patterns. They are powerful tools used in programming and text processing to search, match, and manipulate strings. Think of them as advanced search filters that allow us to find specific patterns within a 5 min read re.match() in Python re.match method in Python is used to check if a given pattern matches the beginning of a string. Itâs like searching for a word or pattern at the start of a sentence. For example, we can use re.match to check if a string starts with a certain word, number, or symbol. We can do pattern matching using 2 min read re.fullmatch() function in Python re.fullmatch() returns a match object if and only if the entire string matches the pattern. Otherwise, it will return None. The flag at the end is optional and can be used to ignore cases etc. Syntax: re.fullmatch(pattern, string, flags=0) Parameters: pattern: the regular expression pattern that you 1 min read Python - Regex Lookahead Lookahead is used as an assertion in Python regular expressions to determine success or failure whether the pattern is ahead i.e to the right of the parser's current position. They don't match anything. Hence, they are called as zero-width assertions. Syntax: # Positive lookahead (?=<lookahead_re 2 min read Pattern matching in Python with Regex You may be familiar with searching for text by pressing ctrl-F and typing in the words youâre looking for. Regular expressions go one step further: They allow you to specify a pattern of text to search for. In this article, we will see how pattern matching in Python works with Regex.Regex in PythonR 8 min read Python | Group strings at particular element in list Sometimes, while working with Python list, we can have a problem in which we have to group strings in a way that at occurrence of particular element, the string list is grouped. This can be a potential problem of day-day programming. Let's discuss certain way in which this problem can be performed. 7 min read Like