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

  • Home
  • SEARCH
  • 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 526871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:49:21+00:00 2026-05-13T08:49:21+00:00

I am working on a math expression parser using regular expressions and I am

  • 0

I am working on a math expression parser using regular expressions and I am trying to add support for parentheses.

My parser works like this:

function parse_expression(expression){
    Find parenthetical expressions
    Loop through parenthetical expressions, call parse_expression() on all of them
    Replace parenthetical expression with value of expression
    Find value of expression
    Return value
}

Because it it recursive, I need to find only the outmost parenthetical expressions. For example if I was parsing the string “(5 + (4 + (3 / 4) + (3 * 2) + 2)) + (1 + 2)”, I want to find the expressions “5 + (4 + (3 / 4) + (3 * 2) + 2)” and “1 + 2”. How do you do this with Regular Expressions?

The regular expression I have now ( “\(([^\)]+)\)” ) would return just “5 + ( 4 + ( 3 * 2”, it doesn’t get the full first expression and it gets none of the second.

Any ideas?

Thanks,

Kyle

  • 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-13T08:49:22+00:00Added an answer on May 13, 2026 at 8:49 am

    Since you’re iterating through it all, I’d say you should still do that, but go the other way around. Find the smallest subsets of paranthetical expressions, rather than the largest ones:

    (\([^(]+\))
    

    Evaluate them, and replace them with their values, i.e., first time round, the matches will be (3 / 4), (3 * 2) and (1 + 2). Replace these with 0,75, 6 and 3, respectively, giving a new string:

    (5 + (4 + 0,75 + 6 + 2)) + 3
    

    And then you iterate that, until there are no more parenthetical expressions, working bottom-up rather than top-down (just like you would manually solve a task like this!)

    Other than that, I agree with all others that exactly what you were asking for should not (indeed could not) be done with regular expressions. But your problem could be solved with this solution that involves regular expressions.

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

Sidebar

Ask A Question

Stats

  • Questions 314k
  • Answers 314k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In throw context arrays decay to pointers. And string literal… May 13, 2026 at 11:02 pm
  • Editorial Team
    Editorial Team added an answer You could use the TextFieldParser. This is technically a VB… May 13, 2026 at 11:02 pm
  • Editorial Team
    Editorial Team added an answer You can adapt the AVStream Filter-Centric Simulated Capture Driver WDK… May 13, 2026 at 11:02 pm

Related Questions

I am working on a Math related web page and am looking for a
I am working on a calculator that allows you to perform calculations past the
I am working on a WISE Installer that needs to run on Java Version
I am working on a .NET application to reduce image file sizes using ImageMagick
I am currently working on a nice little 2D game engine in AS3. You

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.