#!/usr/bin/python import re #import the regular expression module infile = open("test.txt", 'r') lines = infile.readlines() infile.close() # triple quoted string can span multiple lines restring = """[ \t]* # optional whitespace at beginning (?P\w+) # name the matching word 'key' [ \t]*=[ \t]* # equals sign w/ optional whitespace (?P.+) # some non-whitespace after = is value """ matchstr = re.compile(restring, re.IGNORECASE | re.VERBOSE) for line in lines: for match in matchstr.finditer(line): print "key =", match.group("key"), print ", value =", match.group("value")