Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9067185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:53:08+00:00 2026-06-16T16:53:08+00:00

Yesterday I posted a similar question to this one: Python Regex Named Groups .

  • 0

Yesterday I posted a similar question to this one:
Python Regex Named Groups.
This work’s pretty well for simple things.

After some researching I’ve read about the pyparsing library which seems to be pretty perfect for my tasks.

text = '[@a eee, fff fff, ggg @b eee, fff, ggg @c eee eee, fff fff,ggg ggg@d]'
command_s = Suppress(Optional('[') + Literal('@'))
command_e = Suppress(Literal('@') | Literal(']'))
task = Word(alphas)
arguments = ZeroOrMore(
    Word(alphas) + 
    Suppress(
        Optional(Literal(',') + White()) | Optional(White() + Literal('@'))
    )
)
command = Group(OneOrMore(command_s + task + arguments + command_e))
print command.parseString(text)

# which outputs only the first @a sequence
# [['a', 'eee', 'fff', 'fff', 'ggg']]

# the structure should be someting like:
[
     ['a', 'eee', 'fff fff', 'ggg'],
     ['b', 'eee', 'fff', 'ggg'],
     ['c', 'eee eee', 'fff fff', 'ggg ggg'],
     ['d']
]

@ indicates the start of a sequence, the first word is a task (a) followed by optional comma-separated arguments (eee, fff fff, ggg). The problem is, that @b, @c and @d are ignored by the above code. Also “fff fff” getting treated as two separated arguments, it should only be one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:53:09+00:00Added an answer on June 16, 2026 at 4:53 pm

    See the embedded comments.

    text = '[@a eee, fff fff, ggg @b eee, fff, ggg @c eee eee, fff fff,ggg ggg@d]'
    
    from pyparsing import *
    
    LBRACK,RBRACK,AT = map(Suppress,"[]@")
    
    key = AT + Word(alphas)
    
    # use originalTextFor to preserve whitespace between words between commas
    list_item = originalTextFor(OneOrMore(Word(alphas)))
    
    # define a key_value pair using Group to preserve structure
    key_value = Group(key + Optional(delimitedList(list_item)))
    
    parser = LBRACK + OneOrMore(key_value) + RBRACK
    print parser.parseString(text)
    

    This will print your desired output.

    [['a', 'eee', 'fff fff', 'ggg'], 
     ['b', 'eee', 'fff', 'ggg'], 
     ['c', 'eee eee', 'fff fff', 'ggg ggg'], 
     ['d']]
    

    For extra credit, here is how to have pyparsing define keys for you:

    # Extra credit:
    # use Dict to auto-define named groups using each '@x' as a key
    parser = LBRACK + Dict(OneOrMore(key_value)) + RBRACK
    result = parser.parseString(text)
    
    # print the parsed keys
    print result.keys()
    
    # print a value for a particular key
    print result['c']
    
    # print a value for a particular key using object notation
    print result.b
    
    # dump out the whole structure to see just what we got
    print result.dump()
    

    Prints

    ['a', 'c', 'b', 'd']
    ['eee eee', 'fff fff', 'ggg ggg']
    ['eee', 'fff', 'ggg']
    [['a', 'eee', 'fff fff', 'ggg'], ['b', 'eee', 'fff', 'ggg'], ['c', 'eee eee', 'fff fff', 'ggg ggg'], ['d']]
    - a: ['eee', 'fff fff', 'ggg']
    - b: ['eee', 'fff', 'ggg']
    - c: ['eee eee', 'fff fff', 'ggg ggg']
    - d: 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I posted this question yesterday: yesterday's question One of the response was to use
Similar to the question I posted yesterday , I have this problem that I
Yesterday I posted this question regarding using lambdas inside of a Join() method to
This is a followup to a question I posted yesterday. I thought everything was
As a follow-up question to the one posted yesterday, Memory consumption of a pointer
Yesterday, I posted a question on some tips doing this Remote Client-Server Application in
This question is in relation to a question I posted yesterday. The link -http://stackoverflow.com/questions/12636348/how-to-add-a-different-class-to-the-same-class-in-jquery
Yesterday I posted a similar question on why my code started on my content
I posted this question yesterday and the answer I go mentioned that I should
I have posted this problem yesterday..but unfortunately, no one has given me the solution

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.