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

eric eric at ericaro.net
Fri Dec 5 05:56:48 EST 2008


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



More information about the Python-list mailing list