Menu

Clarification on name assignment

2013-09-11
2013-09-11
  • Boris Marin

    Boris Marin - 2013-09-11

    Hello!
    I am trying to parse expression consisting of two elements, where an element can be either an alphanumerical block enclosed in {}, or a number. I am having difficulties using the () to assign names to the matches: when accessing the name corresponding to a block, only the initial '{' is collected. The following snippet illustrates the issue:

    from pyparsing import *
    block = '{' + Word(alphas) + '}'
    number = Word(nums)
    item = block | number
    
    twoitems = item('one') + item('two')
    
    r = twoitems.parseString('{xx} 2')
    r.one, r.two
    

    I would expect r.one to be '{xx}', instead of '{'. What am I missing here?

    Thanks,
    Boris

     
  • Boris Marin

    Boris Marin - 2013-09-11

    Answering my own question, I was able to get the expected result by grouping:

    item = Group(block) | Group(number)
    

    best,
    Boris

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.