Python Regex - re.MatchObject.start() and re.MatchObject.end() functions Last Updated : 01 Jul, 2022 Comments Improve Suggest changes Like Article Like Report 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 whole matched substring). Return -1 if group exists but did not contribute to the match. Return: Index of start of the substring matched by group. AttributeError: If a matching pattern is not found then it raise AttributeError. re.MatchObject.end() This method returns the last index of the substring matched by group. Syntax: re.MatchObject.end([group]) Parameter: group: (optional) group defaults to zero (meaning the whole matched substring). Return -1 if group exists but did not contribute to the match. Return: Index of end of the substring matched by group. AttributeError: If a matching pattern is not found then it raise AttributeError. Consider the below example: Example 1: Python3 import re # getting the match of the string search_pattern = re.search('\d+', '1234') """ d: stands for integer +: means a consecutive set of characters satisfying a condition. Hence d+ will match consecutive integer string """ print(search_pattern.string) print(search_pattern.start()) print(search_pattern.end()) Output: 1234 0 4 Let's understand the code. In the third line of the code we use the re.search() method to find a match in the given string('1234') the 'd' indicates that we are searching for a numeric character and the '+' indicates that we are searching for continuous numeric characters in the given string. The result we get is a re.MatchObject which is stored in search_pattern. If you print search_pattern.string you will get '1234' as output. In the above example, the search_pattern.start() returns value 0 as the index of the first element of the matched string in the given string is 0 and the search_pattern.end() returns 4 that is the end index after the end of the string. Example 2: Python3 import re # getting the match of the string search_pattern = re.search('\d+', 'abcd') """ d: stands for integer +: means a consecutive set of characters satisfying a condition. Hence d+ will match consecutive integer string """ print(search_pattern.start()) print(search_pattern.end()) Output: Traceback (most recent call last): File "/home/4f904f4b53a786e10faa03122533f96b.py", line 13, in print(search_pattern.start()) AttributeError: 'NoneType' object has no attribute 'start' Comment More infoAdvertise with us Next Article Python Regex - re.MatchObject.start() and re.MatchObject.end() functions H haridarshanc Follow Improve Article Tags : Python python-regex Practice Tags : python Similar Reads 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 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.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.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 Python - startswith() and endswith() functions Python provides built-in methods like startswith() and endswith() that are used to check if a string starts or ends with a specific substring. These functions help in string manipulation and validation tasks.Let's understand with an example:Pythons = "GeeksforGeeks" # startswith() # Check if `s` sta 2 min read Python Regex: re.search() VS re.findall() Pythonâs re module provides powerful tools to search, match and manipulate text using regular expressions. Two commonly used functions are re.search(), which finds the first occurrence of a pattern in a string and re.findall(), which retrieves all matches of a pattern throughout the string. Understa 3 min read How Can I Find All Matches to a Regular Expression in Python? In Python, regular expressions (regex) are a powerful tool for finding patterns in text. Whether we're searching through logs, extracting specific data from a document, or performing complex string manipulations, Python's re module makes working with regular expressions straightforward. In this arti 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 Python - Find all close matches of input string from a list In Python, there are multiple ways to find all close matches of a given input string from a list of strings. Using startswith() startswith() function is used to identify close matches for the input string. It checks if either the strings in the list start with the input or if the input starts with t 3 min read How to check a valid regex string using Python? A Regex (Regular Expression) is a sequence of characters used for defining a pattern. This pattern could be used for searching, replacing and other operations. Regex is extensively utilized in applications that require input validation, Password validation, Pattern Recognition, search and replace ut 6 min read Like