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 7431643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:19:40+00:00 2026-05-29T09:19:40+00:00

Given the exampleString = [9+[7*3+[1+2]]-5] How does one extract and store elements enclosed by

  • 0

Given the exampleString = "[9+[7*3+[1+2]]-5]"
How does one extract and store elements enclosed by [] brackets, and then evaluate them in order?

1+2 --+
      |
  7*3+3 --+
          |
        9+24-5

Does one have to create somekind of nested list? Sorry for this somewhat broad question and bad English.

I see, this question is really too broad… Is there a way to create a nested list from that string? Or maybe i should simply do regex search for every element and evaluate each? The nested list option (if it exists) would be a IMO “cleaner” approach than looping over same string and evaluating until theres no [] brackets.

  • 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-05-29T09:19:41+00:00Added an answer on May 29, 2026 at 9:19 am

    Here is a regex solution:

    import re
    
    def evaluatesimple(s):
      return eval(s)
    
    def evaluate(s):
      while 1:
        simplesums=re.findall("\[([^\]\[]*)\]",s)
        if (len(simplesums) == 0):
          break
        replacements=[('[%s]' % item,str(evaluatesimple(item))) for item in simplesums]
        for r in replacements:
          s = s.replace(*r)
      return s
    
    print evaluate("[9+[7*3+[1+2]]-5]")
    

    But if you want to go the whole hog and build a tree to evaluate later, you can use the same technique but store the expressions and sub expressions in a dict:

    def tokengen():
      for c in 'abcdefghijklmnopqrstuvwyxz':
        yield c
    
    def makeexpressiontree(s):
      d=dict()
      tokens = tokengen()
      while 1:
        simplesums=re.findall("\[([^\]\[]*)\]",s)
        if (len(simplesums) == 0):
          break
        for item in simplesums:
          t = tokens.next()
          d[t] = item
          s = s.replace("[%s]"% item,t)
      return d
    
    def evaltree(d):
      """A simple dumb way to show in principle how to evaluate the tree"""
      result=0
      ev={}
      for i,t in zip(range(len(d)),tokengen()):
        ev[t] = eval(d[t],ev)
        result = ev[t]
      return result
    
    s="[9+[7*3+[1+2]]-5]"
    print evaluate(s)
    tree=makeexpressiontree(s)
    print tree
    print evaltree(tree)
    

    (Edited to extend my answer)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the URL (single line): http://test.example.com/dir/subdir/file.html How can I extract the following parts using
Given a abstract factory implementation: public class FooFactory : IFooFactory { public IFoo Create(object
Given the following : // get the list of the players , in order
I'm working on transferring data from one database to another. For this I have
Given this CSS: #gbox_MyGrid .s-ico span { display:none; } How would one apply this
Given a string: String exampleString = example; How do I convert it to an
Hi I have to compute if a given string is substring of a bigger
I have been trying to implement an efficient string comparing algorithm that will given
I have to gather information from a product page which does not have any
I want to create a pattern for the following format of string. I have

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.