Skip to content Skip to sidebar Skip to footer

Matching Against A Large Number Of Strings Containing Spaces In Pyparsing

With pyparsing I need to write a matcher for expressions like a + names + c with a = pp.OneOrMore(pp.Word(pp.alphas)) c = pp.OneOrMore(pp.Word(pp.nums)) and names matching one

Solution 1:

A partial answer:

The spaces problem can be solved with a correct stopOn function:

def last_occurrence_of(expr):
    return expr + ~pp.FollowedBy(pp.SkipTo(expr))

names_kw_list = [pp.Keyword(word, caseless=True)
                                       for word in names_list ]
names = pp.Or(names_kw_list)("names")
a = pp.OneOrMore(pp.Word(pp.alphas), stopOn=last_occurrence_of(names))("A")
c = pp.OneOrMore(pp.Word(pp.nums))("C")

expr = a + names + c 

This instructs a not to eat into the strings of names.

However the performance deteriorates, because now the long list of names is used in a stopOn expression.

Post a Comment for "Matching Against A Large Number Of Strings Containing Spaces In Pyparsing"