Click here to Skip to main content
15,896,730 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i need a regular expression for finding error in 2+6-3+15/3.
error may be start or end with * or / or ++ or -- and between the two number 2++6
Posted
Comments
joshrduncan2012 5-Aug-13 11:12am    
What have you tried to do to accomplish this?

To what end? Why a RegEx and not an expression parser, such as a Reverse Polish Notation parser??
 
Share this answer
 
v2
Bad idea. Regex won't help you here.

Instead, you need to just try to parse the expression and output the syntax errors if some are found. Please see some CodeProject articles:
Math Parser[^],
Math Parser .NET[^],
Parsing Mathematical Expressions with muParser[^],
a Tiny Parser Generator v1.2[^].

You can find some more: http://www.codeproject.com/search.aspx?q=Parser+%22C%23%22&doctypeid=1[^].

There is another approach to this: you can wrap your expression in some valid C# or VB.NET code and try to compile it as an assembly using CodeDOM. Likewise, it can get all the compilation error diagnostics. Please see my past answers:
code generating using CodeDom[^],
Create WPF Application that uses Reloadable Plugins...[^].

But it you do anything like that, it would be better to base all your technology on CodeDOM, not just validation.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900