Q:

re.match returns memory error in python

def name():

    with open('newfile.txt') as f:
        lineno = f.readlines()

    for line in lineno:
        h = re.compile('(#DESIGNATION\ \:[\n\t]*)((.)*[\n\t]*)*?\#')
        print h.match(line)

name()

newfile.txt contains about 100 lines.  When run this program give error MemoryError.  While removing ?  from '(#DESIGNATION\ \:[\n\t]*)((.)*[\n\t]*)*?\#', gives no error.  Why is this happening and what are feasible solutions. 
Thanks. 

A:

If you want to match "#DESIGNATION :" followed by some lines followed by a line with a "#" at the beginning, you first need to read the text in as a single string and use re. MULTILINE to match it.  Here is an example:
import re

text = '''
cat
mouse
#DESIGNATION : horse
dog
bird
lake
#
ocean
sea
#DESIGNATION : bike
box
table
#
nothing
something
'''

h = re.compile('^#DESIGNATION :(?:[ ^\n]|\n[^#])*\n#', re. MULTILINE)
matches = re.findall(h, text)
print repr(matches)

which outputs
['#DESIGNATION : horse\ndog\nbird\nlake\n#', '#DESIGNATION : bike\nbox\ntable\n#']

Note that I'm using the (?:)  match operator here to group regexes together without capturing their matched text each time it is evaluated. 
With a larger file you probably wouldn't want to have re match the entire text body at once and iterate through the lines instead.  If you do that, though, you cannot use '\n' in the expression because you will only be working with a single line at a time.  Instead you would need to maintain state of if you're in a #DESIGNATION block or not.