By default, pyparsing will find the longest match, starting at the first character. So, if your parse is given by num = Word('0123456789')
, parsing either "462"
or "462-780"
will both return the same value. However, if the parseAll=True
option is passed, the parse will attempt to parse the entire string. In this case, "462"
would be matched, but parsing "462-780"
would raise a ParseException, because the parser doens't know how to deal with the dash.
I would recommend constructing something that will match the entirety of the file, then using the parseAll=True
flag in parseFile()
. If I understand your description of each entry being separated by braces correctly, one could do the following.
entire_file = OneOrMore('[' + wildcard + ']')
output = wildcard.parseFile(myfile,parseAll=True)
print output