Match multiline regex in file object
        Posted  
        
            by williamx
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by williamx
        
        
        
        Published on 2010-03-12T15:14:59Z
        Indexed on 
            2010/03/12
            15:17 UTC
        
        
        Read the original article
        Hit count: 574
        
How can I extract the groups from this regex from a file object (data.txt)?
import numpy as np
import re
import os
ifile = open("data.txt",'r')
# Regex pattern
pattern = re.compile(r"""
                ^Time:(\d{2}:\d{2}:\d{2})   # Time: 12:34:56 at beginning of line
                \r{2}                       # Two carriage return
                \D+                         # 1 or more non-digits
                storeU=(\d+\.\d+)
                \s
                uIx=(\d+)
                \s
                storeI=(-?\d+.\d+)
                \s
                iIx=(\d+)
                \s
                avgCI=(-?\d+.\d+)
                """, re.VERBOSE | re.MULTILINE)
time = [];
for line in ifile:
    match = re.search(pattern, line)
    if match:
        time.append(match.group(1))
The problem in the last part of the code, is that I iterate line by line, which obviously doesn't work with multiline regex. I have tried to use pattern.finditer(ifile) like this:
for match in pattern.finditer(ifile):
    print match
... just to see if it works, but the finditer method requires a string or buffer.
I have also tried this method, but can't get it to work
matches = [m.groups() for m in pattern.finditer(ifile)]
Any idea?
© Stack Overflow or respective owner