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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:11:43+00:00 2026-05-30T12:11:43+00:00

I have an arithmetic expression string exp = ((2+3.1)/2)*4.456; I want to validate by

  • 0

I have an arithmetic expression

string exp = "((2+3.1)/2)*4.456";

I want to validate by using regular expression. The expression can only have integers, floating point numbers, operands and parenthesis.

How can i generate regular expression to validate please help or suggest any other way to validate that string.

  • 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-30T12:11:44+00:00Added an answer on May 30, 2026 at 12:11 pm

    Using Perl/PCRE we could verify such simple arithmetic expressions with help of a pattern structured like:

    expr = pnum ( op pnum )*
    pnum = num | \( expr \)
    

    Where num and op defined as required. For example:

    num = -?+\d++(?:\.\d++)?+
    op = [-+*/]
    

    Which would give us the following working expression:

    (?x)^ (?&expr) $
    
    (?(DEFINE)
        (?<expr> (?&pnum) (?: (?&op) (?&pnum) )*+   )
        (?<pnum> (?> (?&num) | \( (?&expr) \) )   )
        (?<num>  -?+\d++(?:\.\d++)?+   )
        (?<op>   [-+*/]   )
    )
    

    But such expressions could not be used with .NET regex as it does not support (recursive) suppatern calls (?&name).
    Instead .NET regex lib offers us its special feature: balancing groups.

    With balancing groups we could rewrite the required recursive call used in pnum, and use a structure like this instead:

    expr = pnum ( op pnum )* (?(p)(?!))
    pnum = (?> (?<p> \( )* num (?<-p> \) )* )
    

    What we’ve done here is to allow any number of optional opening and closing paranthesis before and after every number, counting the total number of open parentheses (?<p> \( ), subtracting closing parentheses from that number (?<-p> \) ) and at the end of the expression make sure that the number of open parentheses is 0 (?(p)(?!)).

    (I believe this is equivalent to the original structure, altho I haven’t made any formal proof.)

    Resulting in the following .NET pattern:

    (?x)
    ^
    (?> (?<p> \( )* (?>-?\d+(?:\.\d+)?) (?<-p> \) )* )
    (?>(?:
        [-+*/]
        (?> (?<p> \( )* (?>-?\d+(?:\.\d+)?) (?<-p> \) )* )
    )*)
    (?(p)(?!))
    $
    

    C# Example:

    using System;
    using System.Text.RegularExpressions;
    
    namespace RegexTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                var expressions = new string[] {
                    "((2+3.1)/2)*4.456",
                    "1",
                    "(2)",
                    "2+2",
                    "(1+(2+3))",
                    "-2*(2+-2)",
                    "1+(3/(2+7-(4+3)))",
                    "1-",
                    "2+2)",
                    "(2+2",
                    "(1+(2+3)",
                };
    
                var regex = new Regex(@"(?x)
                    ^
                    (?> (?<p> \( )* (?>-?\d+(?:\.\d+)?) (?<-p> \) )* )
                    (?>(?:
                        [-+*/]
                        (?> (?<p> \( )* (?>-?\d+(?:\.\d+)?) (?<-p> \) )* )
                    )*)
                    (?(p)(?!))
                    $
                ");
    
                foreach (var expr in expressions)
                {
                    Console.WriteLine("Expression: " + expr);
                    Console.WriteLine("    Result: " + (regex.IsMatch(expr) ? "Matched" : "Failed"));
                }
            }
        }
    }
    

    Output:

    Expression: ((2+3.1)/2)*4.456
        Result: Matched
    Expression: 1
        Result: Matched
    Expression: (2)
        Result: Matched
    Expression: 2+2
        Result: Matched
    Expression: (1+(2+3))
        Result: Matched
    Expression: -2*(2+-2)
        Result: Matched
    Expression: 1+(3/(2+7-(4+3)))
        Result: Matched
    Expression: 1-
        Result: Failed
    Expression: 2+2)
        Result: Failed
    Expression: (2+2
        Result: Failed
    Expression: (1+(2+3)
        Result: Failed
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have an arithmetic expression as a string (2+3*4/5), is there a way
Let's say I have some code that does some floating point arithmetic and stores
Possible Duplicates: Is JavaScript's Math broken? Java floating point arithmetic I have the current
I have seen long articles explaining how floating point numbers can be stored and
I have a project where I want to have checked arithmetic by default, except
I have compiled the NTL inifite precision integer arithmetic library for c++, using Microsoft
I have 2 colors #DCE7FA and #CADBF7. Want the intermediate color(a kind of Arithmetic
i want to do arithmetic operation on jsp . I am using struts tag
I am trying to calculate an arithmetic expression, which is entered as a string
I have a class that encapsulates some arithmetic, let's say fixed point calculations. I

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.