re.MatchObject.span() Method in Python - regex Last Updated : 02 Sep, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 of the matched string. If group did not contribute to the match it returns(-1,-1). AttributeError: If a matching pattern is not found then it raises AttributeError. Consider the below example: Example 1: Python3 # import library import re """ We create a re.MatchObject and store it in match_object variable, '()' parenthesis are used to define a specific group """ match_object = re.match(r'(\d+)', '128935') """ d in above pattern stands for numerical character + is used to match a consecutive set of characters satisfying a given condition so d+ will match a consecutive set of numerical characters """ # generating the tuple with the # starting and ending index print(match_object.span()) Output: (0, 6) It's time to understand the above program. We use a re.match() method to find a match in the given string('128935') the 'd' indicates that we are searching for a numerical character and the '+' indicates that we are searching for continuous numerical characters in the given string. Note the use of '()' the parenthesis is used to define different subgroups. Example 2: If a match object is not found then it raises AttributeError. Python3 # import library import re """ We create a re.MatchObject and store it in match_object variable, '()' parenthesis are used to define a specific group""" match_object = re.match(r'(\d+)', 'geeks') """ d in above pattern stands for numerical character + is used to match a consecutive set of characters satisfying a given condition so d+ will match a consecutive set of numerical characters """ # generating the tuple with the # starting and ending index print(match_object.span()) Output: Traceback (most recent call last): File "/home/18a058de83529572f8d50dc9f8bbd34b.py", line 17, in print(match_object.span()) AttributeError: 'NoneType' object has no attribute 'span' Comment More infoAdvertise with us Next Article re.MatchObject.span() Method in Python - regex H haridarshanc Follow Improve Article Tags : Python python-regex Practice Tags : python Similar Reads 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 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.groups() function in Python - Regex 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 s 2 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 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 Like