Строка между двумя маркерами с использованием PyParsing - PullRequest
2 голосов
/ 09 января 2020

Мне нужно получить строку между двумя маркерами, используя PyParsing.

Из строки s = 'qwertyAAA1234ZZZazerty' Я хочу получить строку между AAA и ZZZ, которая равна 1234.

Пока я могу сделать это, используя searchString(). Можно ли получить тот же результат с помощью parseString()?

с использованием PyStarsing searchString ()

import pyparsing as pp

word = pp.Word(pp.alphanums)

s = 'qwertyAAA1234ZZZazerty'
rule = pp.nestedExpr('AAA', 'ZZZ')
for match in rule.searchString(s):
    print(match)

, что дает:

[['1234']]

Использование parseString () PyParsing *) 1024 *

import pyparsing as pp

word = pp.Word(pp.alphanums)

s = 'gfgfdAAA1234ZZZuijjk'
rule = pp.nestedExpr('AAA', 'ZZZ')
print(rule.parseString(s))

, что дает:

Traceback (most recent call last):
  File "main.py", line 14, in <module>
    print(rule.parseString(s))
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pyparsing.py", line 1939, in parseString
    raise exc
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pyparsing.py", line 1929, in parseString
    loc, tokens = self._parse(instring, 0)
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pyparsing.py", line 1669, in _parseNoCache
    loc, tokens = self.parseImpl(instring, preloc, doActions)
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pyparsing.py", line 4430, in parseImpl
    return self.expr._parse(instring, loc, doActions, callPreParse=False)
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pyparsing.py", line 1669, in _parseNoCache
    loc, tokens = self.parseImpl(instring, preloc, doActions)
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pyparsing.py", line 4430, in parseImpl
    return self.expr._parse(instring, loc, doActions, callPreParse=False)
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pyparsing.py", line 1669, in _parseNoCache
    loc, tokens = self.parseImpl(instring, preloc, doActions)
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pyparsing.py", line 4020, in parseImpl
    loc, resultlist = self.exprs[0]._parse(instring, loc, doActions, callPreParse=False)
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pyparsing.py", line 1673, in _parseNoCache
    loc, tokens = self.parseImpl(instring, preloc, doActions)
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pyparsing.py", line 4430, in parseImpl
    return self.expr._parse(instring, loc, doActions, callPreParse=False)
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pyparsing.py", line 1673, in _parseNoCache
    loc, tokens = self.parseImpl(instring, preloc, doActions)
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pyparsing.py", line 2871, in parseImpl
    raise ParseException(instring, loc, self.errmsg, self)
pyparsing.ParseException: Expected "AAA", found 'g'  (at char 0), (line:1, col:1)

1 Ответ

3 голосов
/ 09 января 2020

Попробуйте SkipTo или его ярлык ...:

print((... + rule).parseString(s).dump())

Дает:

['gfgfd', ['1234']]
- _skipped: ['gfgfd']
...