使用 PyParsing 的两个标记之间的字符串
String between two markers using PyParsing
我需要使用 PyParsing 获取两个标记之间的字符串。
我想从字符串 s = 'qwertyAAA1234ZZZazerty'
中检索 AAA
和 ZZZ
之间的字符串,即 1234
。
到目前为止,我可以使用 searchString()
来完成。 parseString()
是否可以得到相同的结果?
使用 PyParsing 的 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']]
使用 PyParsing 的 parseString()
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)
试试 SkipTo,或者它的快捷方式 ...
:
print((... + rule).parseString(s).dump())
给出:
['gfgfd', ['1234']]
- _skipped: ['gfgfd']
我需要使用 PyParsing 获取两个标记之间的字符串。
我想从字符串 s = 'qwertyAAA1234ZZZazerty'
中检索 AAA
和 ZZZ
之间的字符串,即 1234
。
到目前为止,我可以使用 searchString()
来完成。 parseString()
是否可以得到相同的结果?
使用 PyParsing 的 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']]
使用 PyParsing 的 parseString()
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)
试试 SkipTo,或者它的快捷方式 ...
:
print((... + rule).parseString(s).dump())
给出:
['gfgfd', ['1234']]
- _skipped: ['gfgfd']