
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Use re.finditer() Method in Python Regular Expression
The re.finditer() method in Python's 're' module finds all occurrences of a specified pattern in a string. It returns an iterator containing match objects for each occurrence.
This method is especially useful when you need to determine the position of each match within the input string. Following is the syntax for re.finditer() method.
re.finditer(pattern, string, flags=0)
How to Use 're.finditer()' Method
Following are the steps involved in using re.finditer() method.
- Import the re module: You need to import the regular expression module before using it.
- Compile a pattern: You can compile a regular expression using 're.compile()', but this step is optional.
- Call 're.finditer(pattern, string)': Pass the regular expression pattern and the string to search.
Example
Following is the simple word match example to demonstrate the use of re.finditer() method.
import re text = "The rain in Spain stays mainly in the plain." pattern = r"\bin\b" # Match the word "in" matches = re.finditer(pattern, text) for match in matches: print(f"Match: {match.group()}, Start: {match.start()}, End: {match.end()}")
Output
Following is the output for the above code.
Match: in, Start: 4, End: 6 Match: in, Start: 22, End: 24 Match: in, Start: 36, End: 38
Extracting Dates from a Document
In this scenario, we are going to find and extract date-like patterns from a document using 're.finditer()' method. This is helpful in data processing, where you need to extract dates for further analysis.
Example
In the following example, the regex pattern matches the format of dates (YYYY-MM-DD).
import re text = "Important dates: 2023-01-01, 2024-05-10, and 2025-12-25." pattern = r'\d{4}-\d{2}-\d{2}' matches = list(re.finditer(pattern, text)) for match in matches: start = match.start() end = match.end() print(f'Date found "{match.group()}" at {start}:{end}')
Output
Following is the output of the above code ?
Date found "2023-01-01" at 20:30 Date found "2024-05-10" at 32:42 Date found "2025-12-25" at 48:58
Searching for Multiple Occurrences in Text
We can use re.finditer() method when we need to find all occurrences of a word or phrase in a large block of text.
Example
The following code finds occurrences of the word "cat" and prints its start and end positions in the string.
import re text = "The cat sat on the mat. The cat is happy." pattern = "cat" matches = list(re.finditer(pattern, text)) for match in matches: start = match.start() end = match.end() print(f'Match found "{match.group()}" at {start}:{end}')
Output
Following is the output for the above code.
Match found "cat" at 4:7 Match found "cat" at 31:34