The following prints ‘pass’:
pattern = Word(alphanums) + StringEnd()
str=" bar "
results = pattern.parseString(str)
if 1 == len(results) && "bar" == results[0] :
print("pass")
else:
print("fail")
but this throws and exception in parseImpl of class StringEnd (when the char following ‘r’ in ‘bar’ is not a string end):
pattern = Word(alphanums) + StringEnd()
str=" bar foo "
results = pattern.parseString(str) # <-- exception raised
as does this:
pattern = Word(alphanums)
str = " bar foo"
results = pattern.parseString(str,parseAll=True)
I would expect the 2nd/3rd parseString calls to just return an empty array, as it would obviously not satisfy the grammer.
Can anyone help me understand why?
(should be obvious, but to run you’ll need ‘from pyparsing import *’; also I’m on python3.2 and pyparsing 1.5.6)
Returning an empty list on a failed match is ambiguous – did the grammar match and just return an empty match, or did it not match at all?
Parse some data:
Successful parse, there just weren’t any numbers here:
Exception tells you this match failed: