Within a larger pyparsing grammar, I have something that looks like::
wsj/00/wsj_0003.mrg
When parsing this, I'd like to keep around both the full string, and the
AAA_NNNN substring of it, so I'd like something like::
(['wsj/00/wsj_0003.mrg', 'wsj_0003'], {})
How do I go about this? I was using something like::
... '.mrg')
But of course then all I get back is the full path::
(['wsj/00/wsj_0003.mrg'], {})
I could leave off the final Combine and add a parse action::
... wsj_name = tokens[4]
... return ''.join(tokens) , wsj_name
...
([('wsj/00/wsj_0003.mrg', 'wsj_0003')], {})
But that then allows whitespace between the pieces of the path, which
there shouldn't be::
([('wsj/00/wsj_0003.mrg', 'wsj_0003')], {})
How do I make sure no whitespace intervenes, and still have access to
the sub-expression?
Thanks,
STeVe
wsj/00/wsj_0003.mrg
When parsing this, I'd like to keep around both the full string, and the
AAA_NNNN substring of it, so I'd like something like::
>>foo.parseStri ng('wsj/00/wsj_0003.mrg')
How do I go about this? I was using something like::
>>digits = pp.Word(pp.nums )
>>alphas = pp.Word(pp.alph as)
>>wsj_name = pp.Combine(alph as + '_' + digits)
>>wsj_path = pp.Combine(alph as + '/' + digits + '/' + wsj_name +
>>alphas = pp.Word(pp.alph as)
>>wsj_name = pp.Combine(alph as + '_' + digits)
>>wsj_path = pp.Combine(alph as + '/' + digits + '/' + wsj_name +
But of course then all I get back is the full path::
>>wsj_path.pars eString('wsj/00/wsj_0003.mrg')
I could leave off the final Combine and add a parse action::
>>wsj_path = alphas + '/' + digits + '/' + wsj_name + '.mrg'
>>def parse_wsj_path( string, index, tokens):
>>def parse_wsj_path( string, index, tokens):
... return ''.join(tokens) , wsj_name
...
>>wsj_path.setP arseAction(pars e_wsj_path)
>>wsj_path.pars eString('wsj/00/wsj_0003.mrg')
>>wsj_path.pars eString('wsj/00/wsj_0003.mrg')
But that then allows whitespace between the pieces of the path, which
there shouldn't be::
>>wsj_path.pars eString('wsj / 00 / wsj_0003.mrg')
How do I make sure no whitespace intervenes, and still have access to
the sub-expression?
Thanks,
STeVe
Comment