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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:26:17+00:00 2026-05-14T06:26:17+00:00

For example, these are valid math expressions: a * b + c -a *

  • 0

For example, these are valid math expressions:

a * b + c
-a * (b / 1.50)
(apple + (-0.5)) * (boy - 1)

And these are invalid math expressions:

--a *+ b @ 1.5.0  // two consecutive signs, two consecutive operators, invalid operator, invalid number
-a * b + 1)  // unmatched parentheses
a) * (b + c) / (d  // unmatched parentheses

I have no problem with matching float numbers, but have difficulty with parentheses matching. Any idea? If there is better solution than regular expression, I’ll accept as well. But regex is preferred.

========

Edit:

I want to make some comments on my choice of the “accepted answer”, hoping that people who have the same question and find this thread will not be misled.

There are several answers I consider “accepted”, but I have no idea which one is the best. So I chose the accepted answer (almost) randomly. I recommend reading Guillaume Malartre’s answer as well besides the accepted answer. All of them give practical solutions to my question. For a somewhat rigorous/theoretical answer, please read David Thornley’s comments under the accepted answer. As he mentioned, Perl’s extension to regular expression (originated from regular language) make it “irregular”. (I mentioned no language in my question, so most answerers assumed the Perl implementation of regular expression – probably the most popular implementation. So did I when I posted my question.)

Please correct me if I said something wrong above.

  • 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-14T06:26:17+00:00Added an answer on May 14, 2026 at 6:26 am

    Matching parens with a regex is quite possible.

    Here is a Perl script that will parse arbitrary deep matching parens. While it will throw out the non-matching parens outside, I did not design it specifically to validate parens. It will parse arbitrarily deep parens so long as they are balanced. This will get you started however.

    The key is recursion both in the regex and the use of it. Play with it, and I am sure that you can get this to also flag non matching prens. I think if you capture what this regex throws away and count parens (ie test for odd parens in the non-match text), you have invalid, unbalanced parens.

    #!/usr/bin/perl
    $re = qr  /
         (                      # start capture buffer 1
            \(                  #   match an opening paren
            (                   # capture buffer 2
            (?:                 #   match one of:
                (?>             #     don't backtrack over the inside of this group
                    [^()]+    #       one or more 
                )               #     end non backtracking group
            |                   #     ... or ...
                (?1)            #     recurse to opening 1 and try it again
            )*                  #   0 or more times.
            )                   # end of buffer 2
            \)                  #   match a closing paren
         )                      # end capture buffer one
        /x;
    
    
    sub strip {
        my ($str) = @_;
        while ($str=~/$re/g) {
            $match=$1; $striped=$2;
            print "$match\n";
            strip($striped) if $striped=~/\(/;
            return $striped;
        }
    }
    
    while(<DATA>) {
        print "start pattern: $_";
        while (/$re/g) { 
            strip($1) ;
        }
    }   
    
    __DATA__
    "(apple + (-0.5)) * (boy - 1)"
    "((((one)two)three)four)x(one(two(three(four))))"
    "a) * (b + c) / (d"
    "-a * (b / 1.50)"
    

    Output:

    start pattern: "(apple + (-0.5)) * (boy - 1)"
    (apple + (-0.5))
    (-0.5)
    (boy - 1)
    start pattern: "((((one)two)three)four)x(one(two(three(four))))"
    ((((one)two)three)four)
    (((one)two)three)
    ((one)two)
    (one)
    (one(two(three(four))))
    (two(three(four)))
    (three(four))
    (four)
    start pattern: "a) * (b + c) / (d"
    (b + c)
    start pattern: "-a * (b / 1.50)"
    (b / 1.50)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 369k
  • Answers 369k
  • 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 Memory management has not changed in iPhone. There is still… May 14, 2026 at 6:27 pm
  • Editorial Team
    Editorial Team added an answer They're two distinct words. To "poll" is to ask for… May 14, 2026 at 6:27 pm
  • Editorial Team
    Editorial Team added an answer This is the solution I went for in the end.… May 14, 2026 at 6:27 pm

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.