simplest way to strip a comment from the end of a line?

eric eric at ericaro.net
Fri Dec 5 11:33:19 EST 2008


On Dec 5, 11:56 am, eric <e... at ericaro.net> wrote:
> On Dec 4, 11:35 pm, Paul McGuire <pt... at austin.rr.com> 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()






More information about the Python-list mailing list