This isn't a regex solution, but uses pyparsing instead. Pyparsing helps you construct recursive-descent parsers, and maintains a code structure that is easy to compose, read, understand, maintain, and remember what you did 6-months after you wrote it in the first place.
Download pyparsing at http://pyparsing.sourceforge.net. -- Paul data = """pie=apple,quantity=1,cooked=yes,ingredients='sugar and cinnamon' Pie=peach,quantity=2,ingredients='peaches,powdered sugar' Pie=cherry,quantity=3,cooked=no,price=5,ingredients='cherries and sugar'""" from pyparsing import CaselessLiteral, Literal, Word, alphas, nums, oneOf, quotedString, \ Group, Dict, delimitedList, removeQuotes # define basic elements for parsing pieName = Word(alphas) qty = Word(nums) yesNo = oneOf("yes no",caseless=True) EQUALS = Literal("=").suppress() # define separate pie attributes pieEntry = CaselessLiteral("pie") + EQUALS + pieName qtyEntry = CaselessLiteral("quantity") + EQUALS + qty cookedEntry = CaselessLiteral("cooked") + EQUALS + yesNo ingredientsEntry = CaselessLiteral("ingredients") + EQUALS + quotedString.setParseAction(removeQuotes) priceEntry = CaselessLiteral("price") + EQUALS + qty # define overall list of alternative attributes pieAttribute = pieEntry | qtyEntry | cookedEntry | ingredientsEntry | priceEntry # define each line as a list of attributes (comma delimiter is the default), grouping results by attribute pieDataFormat = delimitedList( Group(pieAttribute) ) # parse each line in the input string, and create a dict of the results for line in data.split("\n"): pieData = pieDataFormat.parseString(line) pieDict = dict( pieData.asList() ) print pieDict ''' prints out: {'cooked': 'yes', 'ingredients': 'sugar and cinnamon', 'pie': 'apple', 'quantity': '1'} {'ingredients': 'peaches,powdered sugar', 'pie': 'peach', 'quantity': '2'} {'cooked': 'no', 'price': '5', 'ingredients': 'cherries and sugar', 'pie': 'cherry', 'quantity': '3'} ''' -- http://mail.python.org/mailman/listinfo/python-list