Ahmed Ghazey
Ahmed Ghazey

Reputation: 489

Validate mathematical Equation

I'm trying to write regular expression which ensure that mathematical equation syntax is true I tried many times but I didn't get what I want.

I need your help to find a regular expression to handle the following cases:

op1 = op2 [+-*/%^] number or op3*

allow nesting brackets

op1 = (op2 [+-*/%^] ( op3 [+-*/%^] op4 ) ) [+-*/%^] op5 or number*

And also allow merge of both Expressions and allow spaces between operands and operations

Upvotes: 0

Views: 2799

Answers (2)

Ahmed Ghazey
Ahmed Ghazey

Reputation: 489

I tried do this with regular Expression but This was very hard, I make Equation validation using Stacks and it works fine

http://scriptasylum.com/tutorials/infix_postfix/algorithms/postfix-evaluation/

Upvotes: 2

Pierluc SS
Pierluc SS

Reputation: 3176

You can do this through balancing groups, you can read the following article for a good demo and explanation of balancing groups.

http://www.codeproject.com/Articles/21183/In-Depth-with-NET-RegEx-Balanced-Grouping

Upvotes: 1

Related Questions