Hi
Your line 3 should be "body = ZeroOrMore(Word(alphas))", right? Or did you already define word=Word(alphas).
Anyway, this is a classic "gotcha" in regular expressions. It always takes the longest string it can that matches the characteristics.
For example if you run it with sentence defined as begin+body you get
['once','upon','a','time','end','story']. In other words the "end story" is matched by the body. Then it comes to the end of the string and goes, "but where's the "end story" that was prophesied." *
It needs something to help it differentiate. D*mmit, man, it's a string-parser, not a mind-reader!!** But if you gave it something to work with:
- from pyparsing import *
-
-
word=Word(alphas)
-
body = ZeroOrMore(word)
-
begin = Keyword('begin story').suppress()
-
end = Keyword('$end story').suppress()
-
-
sentence = begin + body + end
-
print sentence.parseString("begin story once upon a time $end story")
Good luck!
*might be over anthropomorphising the string parser
**might not be an exact Star Trek quote