re.finditer() in Python Last Updated : 04 Jan, 2025 Comments Improve Suggest changes Like Article Like Report re.finditer() method in Python is used to search for all matches of a pattern in a string and return them as an iterator. In this article, we will explore about re.finditer() method in Python.Let's understand this with the help of an example: Python import re text = "GFG, O, B, GFG, O" pattern = "GFG" # Find all occurrences of "apple" in the string matches = re.finditer(pattern, text) for match in matches: print(match.start(), match.group()) Output0 GFG 11 GFG Table of ContentSyntax of re.finditer()Finding Multiple Patterns Using Flags Extracting Dates from TextSyntax of re.finditer()re.finditer(pattern, string)Parameterspattern: The regular expression pattern we are looking for.string: The string where we want to search for the pattern.ReturnThis method returns an iterator that yields match objects for all non-overlapping matches of the pattern in the string.Finding Multiple Patterns We can also search for multiple patterns at once. Here’s an example of finding both "apple" and "banana": Python import re text = "gfg, gg, geeksforgeeks, gfg, gg" pattern = "gfg|geeksforgeeks" matches = re.finditer(pattern, text) for match in matches: print(match.start(), match.group()) Output0 gfg 9 geeksforgeeks 24 gfg Using Flags We can also use flags to modify how the regular expression works. For instance, using the re.IGNORECASE flag makes the search case-insensitive: Python import re text = "GFG, Gfg, gfg" pattern = "gfg" matches = re.finditer(pattern, text, re.IGNORECASE) for match in matches: print(match.start(), match.group()) Output0 GFG 5 Gfg 10 gfg Extracting Dates from TextLet's consider a practical example where we want to extract all the dates in a DD-MM-YYYY format from a block of text. The re.finditer() function searches through the text and returns an iterator of match objects that correspond to the date pattern. The pattern \b\d{2}-\d{2}-\d{4}\b matches any date in the DD-MM-YYYY format. Python import re # Sample text containing dates in DD-MM-YYYY format text = """ The meeting will be held on 12-05-2024. Another important date is 23-06-2023. Remember, the report is due on 07-09-2024. Also, we have a holiday on 25-12-2024. """ # Define the regular expression to match dates in the DD-MM-YYYY format dp = r'\b\d{2}-\d{2}-\d{4}\b' # Use re.finditer() to find all matches matches = re.finditer(dp, text) # Loop through the matches and print their details for match in matches: print(f"Found date: {match.group()} at position {match.start()} to {match.end()}") OutputFound date: 12-05-2024 at position 29 to 39 Found date: 23-06-2023 at position 67 to 77 Found date: 07-09-2024 at position 110 to 120 Found date: 25-12-2024 at position 149 to 159 Comment More infoAdvertise with us Next Article re.finditer() in Python P pragya22r4 Follow Improve Article Tags : Python python Practice Tags : pythonpython Similar Reads re.findall() in Python re.findall() method in Python helps us find all pattern occurrences in a string. It's like searching through a sentence to find every word that matches a specific rule. We can do this using regular expressions (regex) to create the pattern and then use re.findall() to get a list of matches.Let's say 2 min read re.compile() in Python The re.compile() method in Python is used to compile a regular expression pattern into a regex object. Compiling a pattern makes it more efficient when we need to use the same pattern several times, as it avoids re-compiling the pattern each time. Letâs look at how re.compile() works and when we sho 3 min read numpy.find() in Python numpy.core.defchararray.find(arr, substring, start=0, end=None): Finds the lowest index of the sub-string in the specified range. Parameters: arr : array-like or string to be searched. substring : substring to search for. start, end : [int, optional] Range to search in. Returns : An integer array wi 1 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 Python MongoDB - Find MongoDB is a cross-platform document-oriented database program and the most popular NoSQL database program. The term NoSQL means non-relational. MongoDB stores the data in the form of key-value pairs. It is an Open Source, Document Database which provides high performance and scalability along with 3 min read Like