On Dec 5, 11:56 am, eric <[EMAIL PROTECTED]> wrote: > 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
maybe you'd rather replace: splitter = re.compile(r'(?m)^(?P<data>.*?(".*?".*?)*)(?:#.*?)?$') by from reO import * quote = characters('"') # defining the characters used as string sep sharp= string('#') # defining the sharp symbol data = ALL + repeat( group( quote + ALL + quote + ALL ) ) # ALL ( "ALL" ALL)* comment = group(sharp+ALL+END_LINE) # the comment itself xp = flag(MULTILINE=True) + START_LINE + group( data, name="data") + if_exists(comment) splitter = xp.compile() -- http://mail.python.org/mailman/listinfo/python-list