On Dec 4, 11:35 pm, Paul McGuire <[EMAIL PROTECTED]> wrote: > Yowza! My eyes glaze over when I see re's like "r'(?m)^(?P<data>.*? > (".*?".*?)*)(?:#.*?)?$"! >
yeah, I know ... :( ( I love complicated regexp ... it's like a puzzle game for me) > from pyparsing import quotedString, Suppress, restOfLine > > comment = Suppress('#' + restOfLine) > recognizer = quotedString | comment > > for t in tests: > print t > print recognizer.transformString(t) > print > > Prints: > > this is a test 1 > this is a test 1 > > this is a test 2 #with a comment > this is a test 2 > > this is a '#gnarlier' test #with a comment > this is a '#gnarlier' test > > this is a "#gnarlier" test #with a comment > this is a "#gnarlier" test > > For some added fun, add a parse action to quoted strings, to know when > we've really done something interesting: > > def detectGnarliness(tokens): > if '#' in tokens[0]: > print "Ooooh, how gnarly! ->", tokens[0] > quotedString.setParseAction(detectGnarliness) > > Now our output becomes: > > this is a test 1 > this is a test 1 > > this is a test 2 #with a comment > this is a test 2 > > this is a '#gnarlier' test #with a comment > Ooooh, how gnarly! -> '#gnarlier' > this is a '#gnarlier' test > > this is a "#gnarlier" test #with a comment > Ooooh, how gnarly! -> "#gnarlier" > this is a "#gnarlier" test > > -- Paul I didn't knew pyparsing. It's amazing ! thanks -- http://mail.python.org/mailman/listinfo/python-list