0

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

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Ahmed Ghazey
  • 479
  • 1
  • 9
  • 22

2 Answers2

2

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/

Ahmed Ghazey
  • 479
  • 1
  • 9
  • 22
1

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

Pierluc SS
  • 3,138
  • 7
  • 31
  • 44