Python Pyparsing可选字段

时间:2015-06-02 20:54:42

标签: python pyparsing

我目前正在使用pyparsing模块来创建自己的解释器。我目前的代码是

import pyparsing as pp

# To parse a packet with srcip,dstip,srcmac,dstmac
identifier = pp.Word(pp.alphas,pp.alphanums+'_')
ipfield = pp.Word(pp.nums,max=3)
ipAddr = pp.Combine(ipfield+"."+ipfield+"."+ipfield+"."+ipfield)
hexint = pp.Word(pp.hexnums,exact=2)
macAddr = pp.Combine(hexint+(":"+hexint)*5)
ip = pp.Combine(identifier+"="+ipAddr)
mac = pp.Combine(identifier+"="+macAddr)
pkt = ip & ip & mac & mac
arg = "createpacket<" + pp.Optional(pkt) + ">"
arg.parseString("createpacket<srcip=192.168.1.3dstip=192.168.1.4srcmac=00:FF:FF:FF:FF:00>")

当我运行代码的最后一行来解析示例字符串时,我得到如下错误:

File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/pyparsing.py", line 1041, in parseString
  raise exc
pyparsing.ParseException: Expected ">" (at char 13), (line:1, col:14)

有人可以解释这个错误的原因吗?

0 个答案:

没有答案