2011-07-19 44 views
1

我試圖讓pyparsing提取由字符串中可變數量的字組成的子字符串。使用pyparsing提取可變長度的子字符串

下幾乎工程,但失去的子串的最後一個字:

text = "Joe F Bloggs is the author of this book." 
author = OneOrMore(Word(alphas) + ~Literal("is the")) 

print author.parseString(text) 

輸出:

['Joe', 'F'] 

我缺少什麼?

PS:我知道我可以用正則表達式來做到這一點,但是特別想用pyparsing來做,因爲它需要適應已經使用pyparsing編寫的大量工作。

回答

1

你排除模式有來實際筆者字之前:

>>> author = OneOrMore(~Literal("is the") + Word(alphas)) 
>>> print author.parseString(text) 
['Joe', 'F', 'Bloggs'] 
相關問題