Список всегда содержит минимум три элемента. Исходя из этого, я хочу, чтобы pyparsing сгенерировал три группы, первая из которых содержит все слова до двух последних элементов, а последние две группы должны быть двумя последними элементами. Например:
Код: Выделить всё
"one two three four"
Код: Выделить всё
["one two"], "three", "four"
Код: Выделить всё
import pyparsing as pp
data = "one two three four"
grammar = pp.Regex(r"(?P(\w+\W?)+)\s(?P
\w+) (?P\w+)")
print(grammar.parseString(data).dump())
Код: Выделить всё
['one two three four']
- first: one two
- penultimate: three
- ultimate: four
Код: Выделить всё
import pyparsing as pp
data = "one two three four"
word = pp.Word(pp.alphas)
grammar = pp.Group(pp.OneOrMore(word))("first") + word("penultimate") + word("ultimate")
grammar.parseString(data)
Код: Выделить всё
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python2.7/site-packages/pyparsing.py", line 1125, in parseString
raise exc
pyparsing.ParseException: Expected W:(abcd...) (at char 18), (line:1, col:19)
Подробнее здесь: https://stackoverflow.com/questions/309 ... -pyparsing
Мобильная версия