This is possible if you know that regular expression matching will never span a new line.
Then you can just do
for line in file: result = re.finditer(regex, line)
If matches can span multiple lines, you need to read the entire file in memory. Otherwise, as you know, will your match already be held, or if any content continues ahead, is it impossible to match or the match will fail because the file is not read enough?
Edit:
Theoretically, this can be done. The regex engine should check to see if at any time during the match attempt it reaches the end of the current read part of the stream, and if that happens, read on (possibly before EOF). But the Python engine does not.
Edit 2:
I looked at Python stdlib re.py and its related modules. The actual creation of the regex object, including its .match() method and others, is done in extension C. Thus, you cannot access and disable it in order to process streams as well, unless you directly edit C sources and create your own Python version.
source share