regex question
Paul McGuire
ptmcg at austin.rr.com
Sat Jun 25 14:22:07 EDT 2005
Here's a pyparsing version of this, that may be easier to maintain long
term (although if you have your heart set on learning regexp's, they
will certainly do the job). Note that in pyparsing, you don't have to
spell out where the whitespace goes - pyparsing's default logic assumes
that whitespace may be found between any grammar elements, and if
found, it is ignored. (I believe regexp has a special magic symbol
that will do the same thing.)
Download pyparsing at http://pyparsing.sourceforge.net.
-- Paul
import pyparsing as pp
testString = ' 1 2 3'
integer = pp.Word(pp.nums)
lineData = pp.OneOrMore( integer )
results = lineData.parseString( testString )
print results
will print:
['1', '2', '3']
More information about the Python-list
mailing list