Skip to content Skip to sidebar Skip to footer

Pyparsing Or Operation Use Shortest String When More Than Two Match

I need to parse some statements but want the flexibility of using multiple words to signal the of the statement. eg. string = ''' start some statement end other stuff in between st

Solution 1:

SkipTo is one of the less predictable features of pyparsing, as it is easy for input data to be such that it results in more or less skipping than desired.

Try this instead:

term = LineEnd().suppress() | '.' | 'end'statement = 'start' + OneOrMore(~term + Word(alphas)) + term

Instead of skipping blindly, this expression iteratively finds words, and stops when it finds one of your terminating conditions.

If you want the actual body string instead of the collection of words, you can use originalTextFor:

statement = 'start' + originalTextFor(OneOrMore(~term + Word(alphas))) + term

Post a Comment for "Pyparsing Or Operation Use Shortest String When More Than Two Match"