recording data between [ and ]

Paul McGuire ptmcg at austin.rr.com
Thu Apr 21 11:53:20 EDT 2005


Jay -

Thanks for the pyparsing plug.

Here is how the OP's program would look using pyparsing:


import pyparsing

fp = file('filename')
data = fp.read()
fp.close()

foo = '''stuff   [lsass.exe]
[System]  more stuff
xxxxx [firefox.exe] ......
'''

LBRACK = pyparsing.Literal("[").suppress()
RBRACK = pyparsing.Literal("]").suppress()
brackettedStuff = LBRACK + pyparsing.SkipTo( RBRACK ) + RBRACK

for tokens,start,end in brackettedStuff.scanString( foo ):
    print tokens[0]

--- fin ---
Now this is not nearly as terse as the regexp version, nor will it run
as fast.  But I think I'd rather come back to this version 6 months
from now and try to figure "what was this program doing again?".

-- Paul




More information about the Python-list mailing list