import re
name_block = re.compile('<Name>(?P<name>.*)</Name>')
def get_name_block():
#open the file for reading
file_name = "test.txt"
f = open(file_name, 'r')
#look through it one line at a time
name_block = ""
search_result = None
for l in f:
search_result = name_block.match(l)
if search_result:
name_block = search_result.groups[0]
return name_block
That will have a problem if <Name>...</Name> spans over multiple lines, because "for l in f" pulls one line at a time. In that case, no one line would ever match the regex. Hope that works for you!