Here is your sample parser:
from pyparsing import *
stringWord = Word(alphas, alphanums)
# only want words not at the end of the string for the leading part
leadingWord = stringWord + ~LineEnd()
leadingPart = originalTextFor(stringWord + ZeroOrMore(leadingWord))
# define parser, with named results, similar to named groups in a regex
parser = leadingPart("first") + Optional(stringWord, default='')("second")
Here's how it works in practice:
tests = ["aa234",
"aa234 bbb2b ccc ddd eee ",]
for test in tests:
results = parser.parseString(test)
print results.dump()
print results.first
print results.second
Prints:
['aa234', '']
- first: aa234
- second:
aa234
['aa234 bbb2b ccc ddd', 'eee']
- first: aa234 bbb2b ccc ddd
- second: eee
aa234 bbb2b ccc ddd
eee